How to Convert a Currency String to a Double with JavaScript

How to convert a currency string to a double with Javascript?

Remove all non dot / digits:

var currency = "-$4,400.50";
var number = Number(currency.replace(/[^0-9.-]+/g,""));

How to convert price format string into number in jquery

Use String#replace and remove characters which are not a digit or dot.

console.log(  "$2,099,585.43".replace(/[^\d.]/g, ''))

How to format numbers as currency strings

Ok, based on what you said, I'm using this:

var DecimalSeparator = Number("1.2").toLocaleString().substr(1,1);

var AmountWithCommas = Amount.toLocaleString();
var arParts = String(AmountWithCommas).split(DecimalSeparator);
var intPart = arParts[0];
var decPart = (arParts.length > 1 ? arParts[1] : '');
decPart = (decPart + '00').substr(0,2);

return '£ ' + intPart + DecimalSeparator + decPart;

I'm open to improvement suggestions (I'd prefer not to include YUI just to do this :-) )

I already know I should be detecting the "." instead of just using it as the decimal separator...

Price String spliting into amount and currency

If you expect all inputs to include both decimals of the cent value (ie, your comma will always be followed by 2 digits) you could use this:

const amount = money.match(/\d/g).join('') / 100;
const curren = money.match(/[^\d,]/g).join('');

JavaScripts much hated implicit type coercion allows us to divide that string numerator by a number denominator and end up with a number.

To get the currency, we simply extract all non- digit or comma characters and join them.

If you can't rely on the input including the cent value (ie, you might receive a whole dollar amount without a comma or cent digits) try this:

const amount = money.match(/d/g).join('') / (money.includes(',') ? 100 : 1);

Remove currency symbol from string and convert to a number using a single line in Javascript

If the currency symbol will always be there, just use substring:

var priceNum = parseFloat(price.substring(1));

If it may or may not be there, you could use replace to remove it:

var priceNum = parseFloat(price.replace(/£/g, ""));

Beware that parseFloat("") is 0. If you don't want 0 for an empty input string, you'll need to handle that. This answer has a rundown of the various way to convert strings to numbers in JavaScript and what they do in various situations.

Side note: Using JavaScript's standard numbers for currency information is generally not best practice, because if things like the classic 0.1 + 0.2 issue (the result is 0.30000000000000004, not 0.3). There are various libraries to help, and BigInt is coming to JavaScript as well (it's a Stage 3 proposal at the moment, currently shipping in Chrome). BigInt is useful because you can use multiples of your basic currency (for instance, * 100 for pounds and pence).

C# Converting a Currency String to Double

The easiest way to parse the C format is probably with

Double.Parse(text, System.Globalization.NumberStyles.Currency)

Of course you would always want to use Decimal to handle currency, and Decimal.Parse takes the same parameters.

In this case, though, you would want to store your internal numeric values along with their textual representation rather than converting to a string and then parsing back into a number.

javascript converting and exploding string to number

You need to remove the dollar signs and commas, (string replace), then convert to a float value

Try this:

parseFloat('$148,326.00'.replace(/\$|,/g, ''))

See: http://www.w3schools.com/jsref/jsref_parseFloat.asp

Or: http://www.bradino.com/javascript/string-replace/

To handle other currency symbols you could use the following instead (which will remove all non numeric values (excluding a . and -)):

parseFloat('$148,326.00'.replace(/[^0-9.-]+/g, ''))


Related Topics



Leave a reply



Submit