简体   繁体   English

求和而不将其四舍五入到小数点后两位

[英]Getting a sum without rounding it off to two decimal places

Forgive me if I'm too noob about this.如果我对此太菜鸟,请原谅我。 Recently, I post a question regarding the rounding off two decimal places.最近,我发布了一个关于四舍五入小数点后的问题。 Now, How can I get the sum of these numbers but I only need the two decimals w/out rounding it off.现在,我怎样才能得到这些数字的总和,但我只需要两位小数,而不是四舍五入。 This is javascript im working.这是我正在工作的 javascript。

Example: 12.876 + 36.278 = 49.154.示例:12.876 + 36.278 = 49.154。 I need this answer to be... 49.15 only.我需要这个答案是... 49.15 only。 Or another one: 12.876 + 1 = 13.876.或者另一个:12.876 + 1 = 13.876。 I need this answer to be... 13.87我需要这个答案是... 13.87

Here is my code (with round off to two decimal places)这是我的代码(四舍五入到小数点后两位)

function civ(){
civ1=Number(document.addition.scc.value);
civ2=Number(document.addition.ccc.value);
civ3=Number(document.addition.ncc.value);
civ4=Number(document.addition.vch.value);
civ5=Number(document.addition.mch.value);
civ6=Number(document.addition.nlch.value);
civ7=Number(document.addition.slch.value);
valNum1=Math.round((civ1+civ2+civ3+civ4+civ5+civ6+civ7)*10)/10;
document.addition.civ123.value=valNum1;
}

Super thanks to those who are helping me everyday!非常感谢每天帮助我的人! :) :)

Math.floor(N * 100) / 100

Will strip off past two decimal places;将去掉小数点后两位; Math.floor() is essentially Round Down no matter what. Math.floor()本质上都是向下Math.floor()

Math.floor(N * 100) / 100 may not work always. Math.floor(N * 100) / 100 可能并不总是有效。

For Example,例如,

4.56 becomes 4.55 4.56 变成 4.55

If myNumber is the number you want to have two decimals...如果 myNumber 是您想要保留两位小数的数字...

myNumber.toFixed(2)

should work.应该管用。 Source: http://www.w3schools.com/jsref/jsref_tofixed.asp来源: http : //www.w3schools.com/jsref/jsref_tofixed.asp

A very old question, but I saw it didn't have an acceptable answer.一个非常古老的问题,但我看到它没有一个可以接受的答案。 As @user3006769 mentioned, some numbers don't work if you use Math.floor(N*100)/100.正如@user3006769 所提到的,如果您使用 Math.floor(N*100)/100,某些数字将不起作用。

Another approach is to count how many digits there are before the decimal, then convert your number to a string, chop off any characters to the right of the 2nd decimal, then convert it back to a number:另一种方法是计算小数点之前有多少位数字,然后将您的数字转换为字符串,切掉第二个小数点右侧的所有字符,然后将其转换回数字:

function roundDownDecimals(num, decimals) {
  const preDecimalDigits = Math.floor(num).toFixed(0).length;
  return parseFloat(num.toFixed(decimals + 1).slice(0, preDecimalDigits + decimals + 1));
}

roundDownDecimals(4.56, 2);
// returns 4.56

roundDownDecimals(13.876, 2);
// returns 13.87

roundDownDecimals(4.10, 2);
// returns 4.1

If you need to preserve trailing 0's, leave off the parseFloat .如果您需要保留尾随的 0,请不要使用parseFloat

function roundDownDecimals(num, decimals) {
  const preDecimalDigits = Math.floor(num).toFixed(0).length;
  return num.toFixed(decimals + 1).slice(0, preDecimalDigits + decimals + 1);
}

roundDownDecimals(4.10, 2);
// returns "4.10"

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

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