purchases-flutter icon indicating copy to clipboard operation
purchases-flutter copied to clipboard

The price returned is incorrect

Open yuanjiong opened this issue 1 year ago • 5 comments

I created a subscription product and set the price to $39.99. But in the requested data, the price is 39.989999999999995, not 39.99. I need to divide the price by 12 to calculate the price for each month.

StoreProduct(identifier: xxxx, description: xxxxx, title: xxxx, price: 39.989999999999995, priceString: $39.99, currencyCode: USD, introductoryPrice: IntroductoryPrice(price: 0.0, priceString: $0.00, period: P1W, cycles: 1, periodUnit: PeriodUnit.week, periodNumberOfUnits: 1), discounts: [], productCategory: ProductCategory.subscription, defaultOption: null, subscriptionOptions: null, presentedOfferingIdentifier: null, subscriptionPeriod: P1Y)

yuanjiong avatar Feb 18 '24 12:02 yuanjiong

👀 We've just linked this issue to our internal tracker and notified the team. Thank you for reporting, we're checking this out!

RCGitBot avatar Feb 18 '24 12:02 RCGitBot

Can you please confirm the platform where you're seeing this, the version of the SDK you're using, and the system where you're seeing this, that way we can see where the floating point number is losing precision.

Thank you!

vegaro avatar Feb 19 '24 10:02 vegaro

It's not really losing precision. See https://github.com/RevenueCat/purchases-ios/issues/2207#issuecomment-1419737170

That's just an imperfect decimal representation of IEEE 754 floats.

Instead, make sure you use the display methods to properly convert these prices to Strings.

NachoSoto avatar Feb 19 '24 18:02 NachoSoto

I'm seeing this too.

2.9899999999999998, 24.990000000000002,

sgehrman avatar Mar 09 '24 23:03 sgehrman

https://en.wikipedia.org/wiki/IEEE_754

NachoSoto avatar Mar 10 '24 05:03 NachoSoto