English 中文(简体)
至固定 (2) 圆“ x.525” 的圆“ x.525” 不一致性吗?
原标题:toFixed(2) rounds "x.525" inconsistently?

使用固定时我正经历圆形错误 :

我在计算数值时使用了 toFixed(2) ,但少数案例的四舍五入结果不如预期。

假设 toFixed(2) 被应用为值 17.525 ,结果为 17.552 ,如果应用为 5.525 ,结果为 5.53

在后一种情况下,四舍五入结果是准确的, 所以请您建议需要做些什么才能获得后一种情况的准确四舍五入结果。 或者您可以建议替代此固定函数的替代方法, 以获得正确的四舍五入结果?

最佳回答

浮点不准确意味着525结束的多数数字实际上为.52500. 1,而其他数字则为.5249999....

数值周期取决于IEEE-754浮动点中最接近的实际表示值是否高于或低于预期值。

问题回答

而不是 tofixed () 使用 math.ceil () , math. floor () Math.jour ()

环绕着似似似似似似似

var rnum = 5.525,
    decimalPlaces = 2,
    factor = Math.pow(10, decimalPlaces),
    newnumber = Math.round(rnum * factor) / factor,
    mydecimalvalue = parseFloat(newnumber); 

结果为 5.53

将数字转换为字符串并与之合作?

这是在我尝试使用 Math.回合或模拟最近与 Math.ceil 的四舍五入但失败之后的最后手段。 如果乘以100, 某些数字( 如17.525) 将比其值( 1752.5) 少一点100倍, 而其他数字( 如17.545) 则比其值( 1754.5) 多一点100倍( 1754.5) 。

使用 < a href=> https:// developmenter.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NubleFormat/NubleFormat" rel=“Noferrererr”>Intl.Nutionformat ,并在选项集设的 mimimFractDigits 最大FractDigits 和相同数目(要显示的数字数)的选项中使用。

const formatter = [0, 1, 2, 3, 4, 5].map(
    (decimals) =>
        new Intl.NumberFormat( en-US , {
            minimumFractionDigits: decimals,
            maximumFractionDigits: decimals,
        }),
);

console.log(formatter[2].format(17.525)); // 17.53
console.log(formatter[2].format(5.525)); // 5.53
console.log(formatter[2].format(1.005)); // 1.01
console.log(formatter[2].format(8.635)); // 8.64
console.log(formatter[2].format(8.575)); // 8.58
console.log(formatter[2].format(35.855)); // 35.86
console.log(formatter[2].format(859.385)); // 589.39
console.log(formatter[2].format(859.3844)); // 589.38
console.log(formatter[2].format(.004)); // 0.00
console.log(formatter[2].format(0.0000001)); // 0.00

// keep in mind that this will not be formatted as expected, as the value that
// you pass is actually 0.07499999999998863. 
console.log(formatter[2].format(239.575 - 239.5)); // 0.07
console.log(formatter[2].format(0.07499999999998863)); // 0.07




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签