Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert negative number in string to negative decimal in JavaScript

Tags:

javascript

I have a string : "-10.456" I want to convert it to -10.465 in decimal (using JavaScript) so that I can compare for greater than or lesser than with another decimal number.

Regards.

like image 616
Jayaraj PS Avatar asked Oct 24 '14 16:10

Jayaraj PS


2 Answers

The parseInt function can be used to parse strings to integers and uses this format: parseInt(string, radix);

Ex: parseInt("-10.465", 10); returns -10

To parse floating point numbers, you use parseFloat, formatted like parseFloat(string)

Ex: parseFloat("-10.465"); returns -10.465

like image 104
Grice Avatar answered Sep 20 '22 12:09

Grice


Simply pass it to the Number function:

var num = Number(str);
like image 21
Felix Kling Avatar answered Sep 20 '22 12:09

Felix Kling