0

I am trying to convert 1,100.00 or 1,800.00 to 1100 or 1800 by using Javascript Number() function but it gives NaN. For Example:

var num = "1,100.00";
console.log(Number(num));

output: NaN

My requirement is If I have a number 1,800.00 it should convert to 1800 in Javascript.

Any help will be appreciated.

Thanks.

Gufran Hasan
  • 8,910
  • 7
  • 38
  • 51

4 Answers4

4

You can replace the , char using built-in replace function and then just convert to Number.

let number = "1,100.00";
console.log(Number(number.replace(',','')));
lupchiazoem
  • 8,026
  • 6
  • 36
  • 42
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
1

The Number() function converts the object argument to a number that represents the object's value. If the value cannot be converted to a legal number, NaN is returned.

You might have multiple , in the string replace all , from the string before passing to Number():

let numStr = '1,800.00';
let num = Number(numStr.replace(/,/g,''));
console.log(num);
.as-console-wrapper{
  top: 0;
}
Mamun
  • 66,969
  • 9
  • 47
  • 59
0

You can just replace the , with empty '' and then convert the string into number with + operator

let neu = "6,100.00";
document.write(+neu.replace(',',''));
Adel Elkhodary
  • 1,622
  • 1
  • 9
  • 11
0

You can try this.

var myValue = '1,800.00';
var myNumber = myValue.replace(/[^0-9.]/g, '');
var result = Math.abs(myNumber);
console.log( Math.floor(myNumber))

Here, in myValue, only number is taken removing other characters except dot.
Next thing is the value is converted to positive number using Math.abs method.
And at last using Math.floor function the fraction part is removed.

Karthik
  • 112
  • 8