projectfluent / fluent-rs

Rust implementation of Project Fluent
https://projectfluent.org
Apache License 2.0
1.05k stars 95 forks source link

Number printing gives error because of f64 conversion #214

Closed jhoobergs closed 2 months ago

jhoobergs commented 3 years ago

When using variables representing floating numbers, strange situations can arise. The reason for this is https://docs.rs/fluent-bundle/0.14.0/src/fluent_bundle/types/number.rs.html#178 where everything is converted to a f64.

e.g if you use let v : f32 = 300072.3; and pass v as a FluentValue, it will be shown as 300072.3125 because of f64's higher accuracy.

If you would keep it as a f32, however, it would be shown correctly?

zbraniecki commented 3 years ago

Hi! Thanks for reporting!

I'm open to PRs to fix it, and ultimately, we hope to move our numbers formatting to use ICU4X once it gains NumberFormat API.

jhoobergs commented 3 years ago

I renamed the issue, because the problem is bigger. It can also go wrong for whole numbers, because f64 can't represent all whole numbers. e.g. 18014398509481985 is printed as 18014398509481984 (one lower).

(explanation at https://stackoverflow.com/a/19474852)

alerque commented 2 months ago

This issue is a duplicate of #337. That issue is of course later than this one, but since it has some suggestions of a fix I think I'll close this version no discussion continues elsewhere. There is also icu4x stuff in progress (see #269 and #329) that relates to this. I would suggest subscribing to #337 if you want to be notified when this is fixed.