Here's a simple scenario. I want to show the subtraction of two values show on my site:
//Value on my websites HTML is: "75,00"
var fullcost = parseFloat($("#fullcost").text());
//Value on my websites HTML is: "0,03"
var auctioncost = parseFloat($("#auctioncost").text());
alert(fullcost); //Outputs: 75
alert(auctioncost); //Ouputs: 0Can anyone tell me what I'm doing wrong?
011 Answers
This is "By Design". The parseFloat function will only consider the parts of the string up until in reaches a non +, -, number, exponent or decimal point. Once it sees the comma it stops looking and only considers the "75" portion.
To fix this convert the commas to decimal points.
var fullcost = parseFloat($("#fullcost").text().replace(',', '.')); 6 javascript's parseFloat doesn't take a locale parameter. So you will have to replace , with .
parseFloat('0,04'.replace(/,/, '.')); // 0.04 2 parseFloat parses according to the JavaScript definition of a decimal literal, not your locale's definition. (E.g., parseFloat is not locale-aware.) Decimal literals in JavaScript use . for the decimal point.
Why not use globalize? This is only one of the issues that you can run in to when you don't use the english language:
Globalize.parseFloat('0,04'); // 0.04Some links on stackoverflow to look into:
0As @JaredPar pointed out in his answer use parseFloat with replace
var fullcost = parseFloat($("#fullcost").text().replace(',', '.'));Just replacing the comma with a dot will fix, Unless it's a number over the thousands like 1.000.000,00 this way will give you the wrong digit. So you need to replace the comma remove the dots.
// Remove all dot's. Replace the comma.
var fullcost = parseFloat($("#fullcost").text().replace(/\./g,'').replace(',', '.'));By using two replaces you'll be able to deal with the data without receiving wrong digits in the output.
For anyone arriving here wondering how to deal with this problem where commas (,) and full stops (.) might be involved but the exact number format may not be known - this is how I correct a string before using parseFloat() (borrowing ideas from other answers):
function preformatFloat(float){ if(!float){ return ''; }; //Index of first comma const posC = float.indexOf(','); if(posC === -1){ //No commas found, treat as float return float; }; //Index of first full stop const posFS = float.indexOf('.'); if(posFS === -1){ //Uses commas and not full stops - swap them (e.g. 1,23 --> 1.23) return float.replace(/\,/g, '.'); }; //Uses both commas and full stops - ensure correct order and remove 1000s separators return ((posC < posFS) ? (float.replace(/\,/g,'')) : (float.replace(/\./g,'').replace(',', '.')));
};// <-- parseFloat(preformatFloat('5.200,75'))
// --> 5200.75At the very least, this would allow parsing of British/American and European decimal formats (assuming the string contains a valid number).
2Numbers in JS use a . (full stop / period) character to indicate the decimal point not a , (comma).
It is better to use this syntax to replace all the commas in a case of a million 1,234,567
var string = "1,234,567";
string = string.replace(/[^\d\.\-]/g, "");
var number = parseFloat(string);
console.log(number)The g means to remove all commas.
Check the Jsfiddle demo here.
2From my origin country the currency format is like "3.050,89 €"
parseFloat identifies the dot as the decimal separator, to add 2 values we could put it like these:
parseFloat(element.toString().replace(/\./g,'').replace(',', '.')) 0 In my case, I already had a period(.) and also a comma(,), so what worked for me was to replace the comma(,) with an empty string like below:
parseFloat('3,000.78'.replace(',', '')) This is assuming that the amount from the existing database is 3,000.78. The results are: 3000.78 without the initial comma(,).
What you do wrong is feed parseFloat() with strings that represent decimal fractions in a human-oriented notation, whereas parseFloat() accepts only the standard format corresponding with the JavaScript number literals, which are region-independent, always use the dot as the decimal separator, and have no thousands separator.
Futhermore, this parseFloat() function, used in all the answers, is too generous in what it accepts as correct input, preventing the detection of what in most cases is incorrect input:
Input Result
'1Hello' 1
'1 and 2' 1
'1.2+3.4' 1.2
' 25 ' 25In order to get a stricter and therefore better-controlled behavior, I recommend that you implement your own parsing function. Here is mine:
// Parse a decimal fraction with specified thousands
// and group separators:
function /* number */ parse_float
( /* string */ s , // string to parse /* string */ thousep, // thousands separator, empty string if none /* string */ decsep // decimal separator , empty string if none
)
{ var /* integer */ whole, frac ; // whole and fractinal parts var /* integer */ wnext, fnext; // position of next char after parse var /* integer */ fraclen ; // length of fractional part var /* integer */ ofs ; // offset of the first digit var /* boolean */ done ; // entire string scanned? var /* integer */ sign ; // sign of result var /* number */ res ; // result /* labels */ end: { whole: { // Check parameter types and availability: req_param( 's' , s , 'string' ); req_param( 'thousep', thousep, 'string' ); req_param( 'decsep' , decsep , 'string' ); frac = 0; fraclen = 0; res = NaN; // Account for a possible sign: switch( s.charAt(0) ) { case '-': sign = -1; ofs = 1; break; case '+': sign = +1; ofs = 1; break; default : sign = +1; ofs = 0; break; } [done, wnext, whole] = parse_int_ts( s, ofs, thousep ); if( isNaN( whole ) ) break end; if( done ) break whole; if( s.charAt( wnext ) !== decsep ) break end; [done, fnext, frac] = parse_int( s, 0, wnext + 1 ); if( !done ) break end; fraclen = fnext - wnext - 1; if( fraclen === 0 ) break end; } /* whole: */ res = ( whole + frac / Math.pow( 10, fraclen ) ) * sign; } /* end: */ return res;
}
// Require that a value be specified and have the expected type:
function req_param( /* string */ param, /* variant */ val, /* string */ type )
{ var /* string */ errmsg; errmsg = ''; if( val === undefined ) errmsg = 'is undefined'; else if( val === null ) errmsg = 'is null'; else if( typeof val !== type ) errmsg = `must of type \`${type}'`; if( errmsg !== '' ) // there has been an error { throw new Error(`Parameter \`${param}' ${errmsg}.`); }
}
// Parse an integer with a specified thousands separator:
function /* object[] */ parse_int_ts
( /* string */ s , // input string /* integer */ start, // start position /* character */ sep , // thousands separator
)
// Returns an array of:
// 0: boolean -- entire string was scanned
// 1: integer -- index of next character to scan
// 2: integer -- resulting inteer
{ var /* boolean */ full; var /* integer */ next; var /* integer */ res; var /* integer */ len; var /* integer */ psep; var /* integer */ result; res = 0; psep = 0; while( true ) { result = NaN; // mark an error [full, next, res] = parse_int( s, res, start ); len = next - start; if( len === 0 ) break; // nothing parsed if( sep !== '' ) { if( psep > 0 && len !== 3 ) break; // non-first group not 3 digits if( psep === 0 && len > 3 ) break; // first group longer than 3 digits } result = res; // mark success if( s.charAt(next) !== sep ) break; if( full ) break; start = next; psep = next; start = start + 1; } return [full, next, result];
}
// Parse a compact of digits beginning at position `start'
// in string `s' as an integer number:
function /* object[]*/ parse_int
( /* string */ s , // input string /* integer */ init, // initial value /* integer */ start // start position in `s'
)
// Returns an array of:
// 0: boolean -- entire string was scanned
// 1: integer -- index of next character to scan
// 2: integer -- result
{ const /* integer */ ASCII_0 = 48; var /* boolean */ full; // \ var /* integer */ next; // > the return value var /* integer */ res ; // / var /* integer */ n, i; // string length and current position var /* integer */ code; // character code n = s.length; full = true; next = n; res = init; for( i = start; i < n; i += 1 ) { code = s.charCodeAt(i); if( code < ASCII_0 || code >= ASCII_0 + 10 ) { next = i; full = false; break; } res = res * 10 + code - ASCII_0; } if( code === undefined ) res = NaN; return [ full, next, res ];
}And a test program that uses parse_float() to parse numbers of your format:
function test( /* string */ s )
{ var res; res = parse_float( s, ' ', ',' ); console.log(`${(' ' + `[${s}]`).slice(-12)} => ${res}`);
}
test( '' );
test( '12' );
test( '12a' );
test( '12,' );
test( '12,345' );
test( '12 345' );
test( '123 45' );
test( '1 234 567' );
test( '12 345 56' );
test( '12345' );
test( '12 435,678' );
test( '+1,2' );
test( '-2 345' );It writes:
[] => NaN [12] => 12 [12a] => NaN [12,] => NaN [12,345] => 12.345 [12 345] => 12345 [123 45] => NaN [1 234 567] => 1234567 [12 345 56] => NaN [12345] => NaN
[12 435,678] => 12435.678 [+1,2] => 1.2 [-2 345] => -2345 2