Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Currency as string or currency as number?

JSON web service: Whats the best way to output currency?

As a string OR

"Amount": "100.12345"

As an Number?

 "Amount": 100.12345

PayЗal and Google Checkout output json as a string. Why?

like image 607
001 Avatar asked Oct 23 '25 17:10

001


2 Answers

I would do an integer of cents to keep it as simple as possible.

"Amount": 1045

...

var amount = json.amount/100;

...

amount === 10.45
like image 68
David Sawyer Avatar answered Oct 26 '25 06:10

David Sawyer


In my opinion use number until parsing in C# will be to decimal (not double). For more information you can look here Why not use Double or Float to represent currency?

If you are using JSON.NET see this thread Json.NET Primitive Types - use Decimal instead of Double

In JavaScript it doesn't matter what you use.

like image 28
Piotr Stapp Avatar answered Oct 26 '25 06:10

Piotr Stapp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!