-1

I have the code below but I'm having issues when the dollar amount has commas, for example $1,234.56. When I use the code below, it spits out 0.00. It should show the new subtotal with comma(s) if it's over a thousand.

var subtotal = $('.divWithAmount').text().replace("$",""); // Get the subtotal amount and remove the dollar sign
var discount = subtotal * 0.2; // Multiply the amount by 20%
var newSub = subtotal - discount; // Calculate the new subtotal
var newSubtotal = newSub.toFixed(2); // Show only the last two numbers after the decimal
console.log(newSubtotal);

Thanks for your help!

user2428993
  • 323
  • 1
  • 10
  • 1
    Did you try it with removing the $ and commas? – Taplar Apr 23 '18 at 21:34
  • I did try LGson suggestion below but it would great if I can add back the comma(s) if the value of newSubtotal is more than a thousand. – user2428993 Apr 24 '18 at 01:36
  • I suggest you try newer way of doing thing using Number type and with the help of regular expression... – Pranay Rana Apr 25 '18 at 18:02
  • parseFloat("123456789012345.229") will return 123456789012345.23 parseFloat("123456789012345.203") will return 123456789012345.2 .. so ti snot consistent – Pranay Rana Apr 25 '18 at 18:04

2 Answers2

1

The main reason it doesn't work is that the returned value from $('.divWithAmount').text() is of type String

To do operations it needs to be a number, and to enable that you also need to remove the comma and then parse it with e.g. parseFloat().

var subtotal = parseFloat($('div').text().replace("$","").replace(",",""));
var discount = subtotal * 0.2; // Multiply the amount by 20%
var newSub = subtotal - discount; // Calculate the new subtotal
var newSubtotal = newSub.toFixed(2); // Show only the last two numbers after the decimal
console.log(parseFloat(newSubtotal).toLocaleString());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>$51,234.56</div>

As commented, I updated my answer with toLocaleString, so the comma gets added back.

Here is a couple of ways how to localize the end result:

- Javascript Thousand Separator / string format

- Add thousands separator to auto sum

- convert a JavaScript string variable to decimal/money

Asons
  • 84,923
  • 12
  • 110
  • 165
  • LGSon, if the amount in the div is, as an example, $80,234.56, the new amount for newSubtotal is 64187.65. Is there something that needs to be updated so the comma is added back ($64,187.65)? – user2428993 Apr 24 '18 at 01:32
  • @user2428993, for adding the commas, [check this out](https://stackoverflow.com/a/3753507/4770813), apparently there is a built in function for this – Scaramouche Apr 24 '18 at 01:52
  • @user2428993 Updated my answer. – Asons Apr 24 '18 at 04:38
0

to get number out of string value just do like this

var amount = "$1,234.56";
var doublenumber = Number(amount.replace(/[^0-9\.]+/g,""));

once you get number then you can perform operation you want and it will resolve your issue that you are facing.

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263