簡體   English   中英

JavaScript中的Math.round MidPointRounding.AwayFromZero

[英]Math.round MidPointRounding.AwayFromZero in javascript

我想從C#中的這一行在javascript中獲得相同的結果:

round = Math.Round((17245.22 / 100), 2, MidpointRounding.AwayFromZero);
// Outputs: 172.45

我已經嘗試過了,但是沒有成功:

var round = Math.round(value/100).toFixed(2);

提前致謝 :)

如果您知道要潛水100 ,則可以先四舍五入然后除以:

var round = Math.round(value)/100; //still equals 172.45

但是,如果您不知道將要潛水的內容,則可以使用以下更通用的形式:

var round = Math.round(value/divisor*100)/100; //will always have exactly 2 decimal points

在這種情況下, *100將保留Math.round之后的2個小數點,並且/100移動將它們移回到小數點后。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM