Swaragh 013
asked on
Delete digits after two decimal points, without rounding the value in javascript/jquery
Delete digits after two decimal points, without rounding the value in javascript/jquery
var a=987.687
but i need the value like 987.68
how to get value like that
var a=987.687
but i need the value like 987.68
how to get value like that
Why down you just round down? Most languages have two functions to do this both ways, usually called floor() and ceiling() or ceil(). In Javascript they are Math.floor() & Math.ceil()
More info here:
https://www.w3schools.com/jsref/jsref_floor.asp
Annoyingly, Javascript version doesn't allow for the number of digits after the decimal, so you could multiple by 100, floor it, and then divide by 100. Trying to do it with a substring function is that you cannot rely on the length of the number of the number of decimals, so you would have to split it based on the decimal point, crop the second bit, and then join them back together again. The problem with using string functions on numbers can sometimes mean the variable changes type, so you can no longer use it for mathmatics until you force it back to the correct type. Some languages force you to declare their type in advance, but the ones that do it automatically in the background can give you some strange results. I once had an issue where I formatted a number to have a comma separating the 1000's, which turned it into a string, and then did some maths which converted it back, and it assumed the comma was a decimal place.
More info here:
https://www.w3schools.com/jsref/jsref_floor.asp
Annoyingly, Javascript version doesn't allow for the number of digits after the decimal, so you could multiple by 100, floor it, and then divide by 100. Trying to do it with a substring function is that you cannot rely on the length of the number of the number of decimals, so you would have to split it based on the decimal point, crop the second bit, and then join them back together again. The problem with using string functions on numbers can sometimes mean the variable changes type, so you can no longer use it for mathmatics until you force it back to the correct type. Some languages force you to declare their type in advance, but the ones that do it automatically in the background can give you some strange results. I once had an issue where I formatted a number to have a comma separating the 1000's, which turned it into a string, and then did some maths which converted it back, and it assumed the comma was a decimal place.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks for your precious time @leakin971
Open in new window