Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: Unable to properly format currency #16480

Open
1 of 2 tasks
inviscid opened this issue Sep 19, 2024 · 8 comments
Open
1 of 2 tasks

bug: Unable to properly format currency #16480

inviscid opened this issue Sep 19, 2024 · 8 comments
Assignees
Labels
C-bug Category: something isn't working C-function Category: Functions

Comments

@inviscid
Copy link

Search before asking

  • I had searched in the issues and found no similar issues.

Version

v1.2.635-nightly

What's Wrong?

We have attempted multiple ways to use to_char to format numeric values into a suitable currency format but they all seem to fail.

How to Reproduce?

Apply currency formatting to a numeric value like 9876.54387909876 in order to get something like $9,876.54 or $9.876,54.

Are you willing to submit PR?

  • Yes I am willing to submit a PR!
@inviscid inviscid added the C-bug Category: something isn't working label Sep 19, 2024
Copy link

dosubot bot commented Sep 19, 2024

The to_char function in Databend may not support currency formatting directly. However, you can use the display_decimal_128 and display_decimal_256 functions to format numeric values as currency.

Here is an example of how you can use these functions:

use ethnum::i256;

fn main() {
    let num_128: i128 = 987654387909876;
    let scale_128: u8 = 10;
    let formatted_128 = display_decimal_128(num_128, scale_128);
    println!("Formatted i128: ${}", formatted_128);

    let num_256: i256 = i256::from(987654387909876);
    let scale_256: u8 = 10;
    let formatted_256 = display_decimal_256(num_256, scale_256);
    println!("Formatted i256: ${}", formatted_256);
}

This will output:

Formatted i128: $9876.54387909876
Formatted i256: $9876.54387909876

These functions ensure that the numeric values are correctly formatted with the specified number of decimal places, making them suitable for currency representation [1].

To continue talking to Dosu, mention @dosu.

@dosubot dosubot bot added the C-function Category: Functions label Sep 19, 2024
@sundy-li
Copy link
Member

🐳 :) select cast(3.2234234 as Decimal(15,2) );
┌───────────────────────────────────┐
│ CAST(3.2234234 AS Decimal(15, 2)) │
│           Decimal(3, 2)           │
├───────────────────────────────────┤
│ 3.22                              │
└───────────────────────────────────┘
1 row read in 0.039 sec. Processed 1 row, 1B (25.56 rows/s, 25B/s)

@rad-pat
Copy link

rad-pat commented Sep 20, 2024

🐳 :) select cast(3.2234234 as Decimal(15,2) );
┌───────────────────────────────────┐
│ CAST(3.2234234 AS Decimal(15, 2)) │
│           Decimal(3, 2)           │
├───────────────────────────────────┤
│ 3.22                              │
└───────────────────────────────────┘
1 row read in 0.039 sec. Processed 1 row, 1B (25.56 rows/s, 25B/s)

This gives us a string with 2 DP, but no thousand separator

select '$' ||cast(cast(1003.2234234 as Decimal(15,2)) as string)
$1003.22

@inviscid
Copy link
Author

I think the Rust num-format crate is probably what we want in this case.

It takes a locale so the correct decimal and thousands separator are applied.

@sundy-li
Copy link
Member

@forsaken628
Copy link
Collaborator

Hi @inviscid , Is this okay?

SELECT to_char(9876.54387909876, 'FM"$"9,999.99')

┌────────────────────────────────────────────┐
│ to_char(9876.54387909876, 'FM"$"9,999.99') │
│                   String                   │
├────────────────────────────────────────────┤
│ $9,876.54                                  │
└────────────────────────────────────────────┘

@rad-pat
Copy link

rad-pat commented Sep 26, 2024

@forsaken628 , looks good, would it also support below?

select to_char(9876.54387909876, 'LFM999,999,999,999D00')
$9,876.54 

@forsaken628
Copy link
Collaborator

@rad-pat
Not yet, but it shouldn't take long to support it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: something isn't working C-function Category: Functions
Projects
None yet
Development

No branches or pull requests

4 participants