We want to display dynamic value in a table using JavaScript in vb. we have define the variable as var rValue
.
The expression is rValue = parseFloat(594 * 0.1);
We are getting putput as 59.400000000000006
but we need the output as 59.4
please let us know how to do that .
P.S. We do not want to use tofixed()
as it will return the two decimal point for all the values which do not have decimal points.
We want to display dynamic value in a table using JavaScript in vb. we have define the variable as var rValue
.
The expression is rValue = parseFloat(594 * 0.1);
We are getting putput as 59.400000000000006
but we need the output as 59.4
please let us know how to do that .
P.S. We do not want to use tofixed()
as it will return the two decimal point for all the values which do not have decimal points.
-
"
[toFixed()] will return the two decimal point
"??? In which browser? – Teemu Commented Apr 14, 2014 at 6:52 - possible duplicate of Is there a definitive solution to javascript floating-point errors? – Qantas 94 Heavy Commented Apr 14, 2014 at 7:00
6 Answers
Reset to default 2Use rValue = parseFloat(594 * 0.1).toFixed(2)
and if the decimal is .00 remove the decimal places.
e.g.
var num=rValue.split('.')[1];
if(num==='00')
{
rValue=rValue.split('.')[0];
}
try this:
var rValue = parseFloat(594 * .2);
alert(rValue.toFixed(2).replace(".00",""))
here is demo
Use the native toPrecision()
method:
<script>
var rValue = new Number(59.400000000000006 );
var n = rValue.toPrecision(3);
//n will be 59.4
</script>
The problem arises from 0.1 not being able to be represented in binary. It actually has an infinite number of decimals, hence every arithmetic operation with this number results in the output being approximate.
You can either opt to use division by a float 10 instead:
var rValue = 594 / 10.0;
or, if this kind of truncation is a recurring issue, you can write a utility function:
/**
* @param {Number} value
* @param {Integer} precision
* @returns {Number}
*/
var truncateFloat = function(value, precision) {
// Ensure the multiplier is a float
var pMult = 1.0;
while (precision--) {
pMult *= 10;
}
// Multiply the value by the precision multiplier,
// convert it to int (discarding any pesky leftover decimals)
// and float-divide it by the same multiplier
return ((value * pMult) >> 0) / pMult;
}
Hope this helps a little! :)
easy arrow function:
const truncate = (number,decimals) =>
Math.trunc(number*Math.pow(10,decimals)) / Math.pow(10,decimals)
Like This ?
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim rValue As String = 56.40000000006
Dim charVal() As Char = rValue.ToCharArray()
Dim getVal As String = Nothing
'----------Change for decimal places---------
Dim decimalPlaces As Integer = 2
Dim decimalPlace As Integer = -1
Dim realValue As Double = 0
For Each Val As Char In charVal
If Char.IsDigit(Val) Then
getVal &= Val
Else
getVal &= "."
End If
If getVal.Contains(".") Then
decimalPlace += 1
End If
If decimalPlace = decimalPlaces Then
Exit For
End If
Next
MsgBox(realValue)
End Sub