4

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?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
001
  • 62,807
  • 94
  • 230
  • 350
  • It depends on the use case. String + is concatenation, although some shopkeeper may enjoy that $3 item plus $4 item is $34 the joke will only be funny one time. The only bad thing about numbers is they often need rounding. Still I would go with number. – Paul Aug 14 '13 at 04:11

2 Answers2

2

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

"Amount": 1045

...

var amount = json.amount/100;

...

amount === 10.45
David Sawyer
  • 558
  • 1
  • 4
  • 18
1

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.

Community
  • 1
  • 1
Piotr Stapp
  • 19,392
  • 11
  • 68
  • 116