How to replace comma with a dot in the number (or any replacement)
As replace()
creates/returns a new string rather than modifying the original (tt
), you need to set the variable (tt
) equal to the new string returned from the replace
function.
tt = tt.replace(/,/g, '.')
JSFiddle
How do I separate a currency symbol from a money value and preserve decimals and commas?
This should do the trick for you. The regular expression /^(\D*)(.*)/
looks for all non-digit characters at the front of the string and saves it in the first matching group and then puts the remainder of the string in the second matching group.
const regex = /^(\D*)(.*)/;
function x() {
displayAmount = 1234.56789
currencyFormatted = displayAmount.toLocaleString('en-US', {style: 'currency', currency: 'USD'});
m = regex.exec(currencyFormatted );
myElement.innerHTML = "<span>" + m[1] + "</span> " + m[2];
}
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).
unable to remove comma with "replace" in javascript
The problem is because you're only using the result of the second replace()
call - you don't use the value created after you make the first replacement to remove the ,
characters.
That being said you can improve the logic by making a single replace()
call which finds any character that isn't a digit or .
and removes it. Then the formatting function will work fine. Try this:
$("#bob").blur(function() { var numberStripped = $("#bob").val().replace(/[^\d.]/g, ''); var currencyValue = parseFloat(numberStripped).toLocaleString('en-US', { style: 'currency', currency: 'USD', minimumFractionDigits: 2, maximumFractionDigits: 2 }); $('#bob').val(currencyValue);});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><input type="text" name="CurrencyTest" id="bob" maxlength="15">
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...
How can I parse a string with a comma thousand separator to a number?
Yes remove the commas:
let output = parseFloat("2,299.00".replace(/,/g, ''));
console.log(output);
With a browser, how do I know which decimal separator does the operating system use?
Here is a simple JavaScript function that will return this information. Tested in Firefox, IE6, and IE7. I had to close and restart my browser in between every change to the setting under Control Panel / Regional and Language Options / Regional Options / Customize. However, it picked up not only the comma and period, but also oddball custom things, like the letter "a".
function whatDecimalSeparator() {
var n = 1.1;
n = n.toLocaleString().substring(1, 2);
return n;
}
function whatDecimalSeparator() { var n = 1.1; n = n.toLocaleString().substring(1, 2); return n;}
console.log('You use "' + whatDecimalSeparator() + '" as Decimal seprator');
Strip all non-numeric characters from string in JavaScript
Use the string's .replace
method with a regex of \D
, which is a shorthand character class that matches all non-digits:
myString = myString.replace(/\D/g,'');
Related Topics
Syntaxerror: Cannot Use Import Statement Outside a Module
How to Change Options Based on Another Select Option in a Table
How to Check If Value Is in Array With Angularjs
Converting Json Object to CSV Format in JavaScript
Why New Date().Toisostring() Is Loosing Timezone
Remove Specific Characters from String Jquery
Reinitialize/Reset Dropzone After Form Is Submitted
Removing Currency Symbol and Replacing Comma With Point Using Pure JavaScript
Why Isnt Window.Location.Href= Not Forwarding to Page Using Safari
How to Access a Model Attribute With JavaScript Variable
Edit Table Row Inline on Click of Edit in Angular
How to Intercept Button Click Inside Uiwebview on Ios
How to Make Mongoose Not Insert Empty Array or Object Fields into a Document
Node Js - Function to Return Array of Objects Read from Sequelize Database