简体   繁体   English

如何在javascript中截断浮点值

[英]how to truncate the float value in javascript

We want to display dynamic value in a table using JavaScript in vb.net.我们想在 vb.net 中使用 JavaScript 在表格中显示动态值。 we have define the variable as var rValue .我们已将变量定义为 var rValue

The expression is rValue = parseFloat(594 * 0.1);表达式为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 .我们得到的 putput 为59.400000000000006但我们需要输出为59.4请让我们知道如何做到这一点。

PS 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. PS 我们不想使用tofixed()因为它会为所有没有小数点的值返回两个小数点。

Use the native toPrecision() method: 使用本机的toPrecision()方法:

 <script>   
 var rValue = new Number(59.400000000000006 );
 var n = rValue.toPrecision(3);
 //n will be 59.4 
 </script>

Use rValue = parseFloat(594 * 0.1).toFixed(2) and if the decimal is .00 remove the decimal places. 使用rValue = parseFloat(594 * 0.1).toFixed(2) ,如果小数为.00,则删除小数位。

eg 例如

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 这是演示

The problem arises from 0.1 not being able to be represented in binary . 问题是由于0.1无法用二进制表示 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: 您可以选择使用浮点数10代替:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM