0

We have a price, say 5000,00 kr per item and we have two items. I calculate the new price to 10000 something like this:

var newPrice = parseInt(originalPrice) * parseInt(amount);

But I would like to keep the ,00 part and the currency in this case kr.

How would I go about to achieveme this the easiest?

halliewuud
  • 2,735
  • 6
  • 30
  • 41
  • possible duplicate of [How can I format numbers as money in JavaScript?](http://stackoverflow.com/questions/149055/how-can-i-format-numbers-as-money-in-javascript) – cfs Sep 09 '13 at 11:26
  • 1
    use `parseFloat` instead – slash197 Sep 09 '13 at 11:26

2 Answers2

2

Try using toFixed(2)

var newPrice = (parseInt(originalPrice) * parseInt(ammount)).toFixed(2);
Anton
  • 32,245
  • 5
  • 44
  • 54
0

If you just want to append the ,00 and the currency kr you can do

newPrice += ",00 kr"

But you probably don't want to add the ,00 if there already is a decimal part. So instead you should do this

newPrice = newPrice.toFixed(2) + " kr"

This way 40 would be turned into 40.00 kr and 19.9 would be 19.90 kr.

And as @slash197 mentioned in the comments you'd have to parse the string using parseFloat instead of parseInt. Otherwise the decimal part will be thrown away from the original string.

paldepind
  • 4,680
  • 3
  • 31
  • 36