Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the best practices passing dollar amounts in JSON?

What are the best practices passing dollar amounts in JSON?

Passing the amounts as strings, or floats? or other?

We are a bit worried about round off errors displayed on client (iOS / Android / Web), or possibly different values displayed on one client compared to another.

like image 537
Mary Rogers Avatar asked Jul 20 '17 18:07

Mary Rogers


1 Answers

I think one solution is to pass the amount times 100 as an integer

  • $100 --> 10000
  • $1.5 --> 150
  • $19.99 --> 1999

(No rounding errors, safe storage in database, unless you need more decimals if you use this in currency exchange market for example).

That way you can manipulate your amounts (addition or multiplication,..), and to display it juste divide again by 100.

like image 117
Majid Laissi Avatar answered Nov 10 '22 00:11

Majid Laissi