0

I'm so sorry, but I need help :(

I've tried to convert string to decimal and it works, but I have some problem:

    number = document.getElementById("totalcost").innerHTML;    //It is a string, but I am sure that it is a decimal
    number2 = prodCost;  //it is a string but in fact it is a decimal too
    alert(parseFloat(number)); // prints good (if number is 88,9 it will print 88,9)
    alert(parseFloat(number2)); // it's ok too

    alert(parseFloat(number) - parseFloat(number2)); // this is not ok :(
    //if number=88,9 and number2=17,77 I get 71 but i need 71,13

Oh, guys, I'm so sorry, I'm stupit. Thank you so much! I have been working for the 9th hour in a row.. I'm really sorry, thank you all!

  • Do you actually have `,` in your numbers? – Tibos Mar 11 '14 at 13:37
  • possible duplicate of [JavaScript parseFloat in Different Cultures](http://stackoverflow.com/questions/12694455/javascript-parsefloat-in-different-cultures) – Ted Hopp Mar 11 '14 at 13:37

4 Answers4

3

This looks like a locale problem: parseFloat only recognizes a period as a decimal point; it stops parsing when it gets to the comma, giving you only integer values. Unfortunately, there is no way to change this behavior. You need to replace the commas with periods in your number strings in order to get a decimal number.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • This seems to contradict OP's observations (implying he doesn't actually use , as a decimal separator): > alert(parseFloat(number)); // prints good (if number is 88,9 it will print 88,9) – Tibos Mar 11 '14 at 13:38
  • @Tibos - I agree. I suspect, though, that OP is actually executing `alert(number)` instead of `alert(parseFloat(number))` and only using `parseFloat` when subtraction is required. – Ted Hopp Mar 11 '14 at 13:41
  • I'm so sorry, I'm stupit. Thank you so much! – user3406280 Mar 11 '14 at 13:48
  • @user3406280 - There's nothing stupid about it. In my view, this is a flaw in JavaScript. (There is the `Intl` object--see the [ECMAScript Internationalization API](http://www.ecma-international.org/ecma-402/1.0/)--to support formatting numbers in a locale-sensitive way, but nothing to go the other way.) – Ted Hopp Mar 11 '14 at 14:00
0

If you will use dot instead of comma(Example: 71.13 instead of 71,13) everything will work as expected

Nazar Harasym
  • 43
  • 2
  • 8
0

parseInt and parseFloat takes first valid number in the String supplied.

, is not valid

parseFloat("17,77".replace(",","")); //1777

should do the trick, if comma was used a a seperator.

or if comma was used as a decimal point

parseFloat("17,77".replace(",",".")); //17.77

Explained in MDN here

If parseInt encounters a character that is not a numeral in the specified radix, it ignores it and all succeeding characters and returns the integer value parsed up to that point. parseInt truncates numbers to integer values. Leading and trailing spaces are allowed.

sabithpocker
  • 15,274
  • 1
  • 42
  • 75
0

Seem like your need is to manipulate number with these criteria

  • decimal separator is ',' instead of '.', like 1,2345 (one dot two three four five)
  • group separator is '.' instead of ',' like 1.000,1 (a thousand dot one)

You can use numeral.js to manipulate this need.

Try to go to http://numeraljs.com/ and open chrome dev, and then try to play by yourself with these sample code

// declare fr lang
numeral.language('fr', {
    delimiters: {
        thousands: ' ',
        decimal: ','
    },
    abbreviations: {
        thousand: 'k',
        million: 'm',
        billion: 'b',
        trillion: 't'
    },
    ordinal : function (number) {
        return number === 1 ? 'er' : 'ème';
    },
    currency: {
        symbol: '€'
    }
});

numeral.language('fr'); // set fr lang

number = '88,9';
number2 = '17,77';

numberRaw = numeral().unformat(number); // convert string to number
numberRaw2 = numeral().unformat(number2); // convert string to number

resultRaw = numberRaw - numberRaw2; // calculate result

resultStr = numeral(resultRaw).format('0,0.00'); // format to string

console.log(resultStr); // print 71,13
Alongkorn
  • 3,968
  • 1
  • 24
  • 41