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

Incorrect string formatting when rounding up to include additional digit #178

Open
jstol opened this issue May 6, 2022 · 3 comments
Open

Comments

@jstol
Copy link

jstol commented May 6, 2022

I've run into some strange behaviour when converting values to strings that require rounding up. For example, see the code snippet below:

var b decimal.Big
b.SetString("9.996208266660")
fmt.Println(fmt.Sprintf("%.2f", &b)) // Should be 10.00, but is 1.000

b.SetString("0.9996208266660")
fmt.Println(fmt.Sprintf("%.2f", &b)) // Should be 1.00, but is 0.100

Other values appear to be fine – this appears to only happen when rounding up to include an additional digit, almost as if the decimal point is being outputted 1 position left from where it should be. Any idea why this may be happening? More examples can be found here. Thanks!

@lotodore
Copy link
Contributor

I just noted I'm also having this issue and it seems like a serious problem. Rounding up to include an additional digit happens all the time and causes some really strange effects on the UI. :-(

@lotodore
Copy link
Contributor

Workaround, use Quantize on a copy:

var b decimal.Big
b.SetString("9.996208266660")
fmt.Println(fmt.Sprintf("%.2f", new(decimal.Big).Copy(&b).Quantize(2)))

But I really think this should be fixed.

@ethicnology
Copy link

Thanks for the workaround.

Any news on this issue ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants