Reputation: 105
I need to convert a formatted number to JS default number format.
This is my code:
String.prototype.toJsFloatFormat = function() {
debugger;
var newVal = this;
return newVal;
}
//Example of use
var input = 10000.22; //default js format
var formatted = input.toLocaleString("es"); // result is: 10.000,22
var unformatted = formatted.toJsFloatFormat(); //expected result = 10000.22;
The problem is when I need to get the formatted number (10.000,22) and I make operations with this formatted number (parseFloat(10.000,22) + 1000) I have bad results ( parseFloat(10.000,22) + 1000 = 1010)
thanks in advance.
Upvotes: 5
Views: 11930
Reputation: 344
If you need the answer from James Long but in TypeScript, here it is :)
/**
* This function will unformat a number that has been formatted using the passed locale
* @param {string} number - The formatted number to unformat.
* @param {string} locale - The locale used to format the number.
* @returns {number} - Will return the unformatted number in case the conversion was successful,
* otherwise it will return the original number using parseFloat to convert it to type number.
*/
export const unformatLocaleStringifyNumber = (
number: string,
locale: string,
): number => {
const parts = (1234.5).toLocaleString(locale).match(/(\D+)/g);
let unformatted = number;
if (parts) {
unformatted = unformatted.split(parts[0]).join('');
unformatted = unformatted.split(parts[1]).join('.');
return parseFloat(unformatted);
}
return parseFloat(number);
};
Upvotes: 0
Reputation: 509
My functions for format and unFormat currency numbers to 'en-US'. I hope helps
function myFormatPrice(num,digits){
return num.toLocaleString('en-US', {maximumFractionDigits:digits});
}
function myUnFormatPrice(formated){
return parseFloat( formated.replaceAll(',','') );
}
Upvotes: -2
Reputation: 31
I did this, that's fine for me
function localeStringToFloat(locale){
if(!locale) return locale
let test=1000
test=test.toLocaleString(undefined,{minimumFractionDigits: 2,maximumFractionDigits: 2});
let separator=test[1]
let decimalSeparator=test[5]
return parseFloat(locale.replaceAll(separator,'').replace(decimalSeparator,'.'))
}
Upvotes: 1
Reputation: 1098
I did it this way(in my case it was the 'ru' local format, so I did replace the 'space' symbol):
var myNumber = 1000000;
var formated = myNumber.toLocaleString('ru');
var unformated = parseInt(formated.replace(/\s/g, ''));
your case:
var formated = myNumber.toLocaleString('en');
var unformated = parseInt(formated.replace(/,/g, ''));
Upvotes: 2
Reputation: 45947
no need to reinvent the wheel - https://github.com/globalizejs/globalize#readme
var input = 10000.22;
Globalize.parseFloat(input );
Upvotes: 2
Reputation: 4736
It's not easy. There's a reason why most of the comments have said "Don't try - do your calculations on the number itself, not the formatted value".
You need to work out what the decimal and thousand separator characters are. For that, you will need to know which locale the number was converted into.
(1234.5).toLocaleString("es").match(/(\D+)/g);
// -> [".", ","]
Once you have that, you can replace characters in the formatted string.
function unformatString(string, locale) {
var parts = (1234.5).toLocaleString(locale).match(/(\D+)/g);
var unformatted = string;
unformatted = unformatted.split(parts[0]).join("");
unformatted = unformatted.split(parts[1]).join(".");
return parseFloat(unformatted);
}
There is no way of working out the locale - you have to know it and pass it to the function.
Upvotes: 11