Skip to main content
Answer

Prices off by 1 cent (ish)

  • September 21, 2021
  • 7 replies
  • 253 views

Forum|alt.badge.img+3
  • Dedicated Member

When I use the price string from the API, we see 12.99 like we have in the Apple app store. However, when I access the price itself in the Flutter client library, I see 12.989999771118164. I am not manipulating this price at all, I am printing it directly from Flutter lib. Do you think this is: 1) Apple weirdness 2) RevenueCat API bug OR 3) Flutter bug?

Best answer by ryan

This is due to the nature of floating point decimals. More info in this Stackoverflow post here: https://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate

Rounding the float to 2 decimals should land you with the correct formatted currency. There may be some enhancement we could do in the SDK to improve how these are displayed. @taquitos knows more about floating point accuracy than me...

This post has been closed for comments

7 replies

ryan
RevenueCat Staff
Forum|alt.badge.img+9
  • RevenueCat Staff
  • Answer
  • September 21, 2021

This is due to the nature of floating point decimals. More info in this Stackoverflow post here: https://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate

Rounding the float to 2 decimals should land you with the correct formatted currency. There may be some enhancement we could do in the SDK to improve how these are displayed. @taquitos knows more about floating point accuracy than me...


Forum|alt.badge.img+3
  • Author
  • Dedicated Member
  • September 21, 2021

Rounding something like 5.999 even to 2 decimals takes you to “6” so it won’t always work. Maybe in this case though


ryan
RevenueCat Staff
Forum|alt.badge.img+9
  • RevenueCat Staff
  • September 22, 2021

Rounding something like 5.999 even to 2 decimals takes you to “6” so it won’t always work. Maybe in this case though

I don’t think you would run into that, since $5.99, converted to a float should still round back to $5.99. If it was $6.00 then you could see a case of 5.9999 or 6.0001.


Forum|alt.badge.img+4
  • Active Member
  • November 6, 2024

I’m 3 years late, but a search showed this thread.

I have more or less the same issue. My 19.99 subscription shows as 19.98999999… I know about the float issue, but how can I fix this?

I found this, but how is it used?
https://github.com/RevenueCat/purchases-ios/pull/4132

Thanks,


Forum|alt.badge.img+4
  • Active Member
  • November 7, 2024

I expected a RC method to do this. Didn’t find anything (probably because there isn’t such a thing). I did it manually inside the offerings section of my code:

let formatter = NumberFormatter()
formatter.maximumFractionDigits = 2
formatter.minimumFractionDigits = 2
formatter.numberStyle = .decimal
...
let price = formatter.string(for: product.price)!

 


kaitlin
RevenueCat Staff
Forum|alt.badge.img+6
  • RevenueCat Staff
  • November 7, 2024

Hey @geohei,

The GitHub PR that you linked is specific to our Paywalls feature, so may not be relevant if you are formatting your own custom paywalls.

It sounds like you may already have a solution, but the StoreProduct class in our iOS native SDK does have some properties that provide a formatted and localized price. You can find more info about these properties in our SDK reference here: https://revenuecat.github.io/purchases-ios-docs/5.6.0/documentation/revenuecat/storeproduct

 


Forum|alt.badge.img+4
  • Active Member
  • November 9, 2024

Hello @kaitlin.

.localizedPriceString works exactly as desired.
Many thanks for the kick!