- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将两个十进制数相加(参数可以是数字,也可以是解析前的数字字符串)并将结果与 resultInput
进行比较。问题在于系统不能足够准确地表示 float 。例如,0.1 + 0.2 = 0.30000000000000004
。因此,我正在尝试使用 toFixed()
方法使用定点表示法来格式化数字。我在运行代码时收到 false
。不知道我在哪里弄错了。如果您有任何想法,请告诉我。
function calc(firstNumber, secondNumber, operation, resultInput) {
let a = parseFloat(firstNumber); //Number()
let b = parseFloat(secondNumber); //Number()
let c;
let d = parseFloat(resultInput);
console.log(JSON.stringify(`value of d : ${d}`)); //"value of d : NaN"
switch (operation) {
case '+':
c = a + b;
break;
case '-':
c = a - b;
break;
case '*':
c = a * b;
break;
case '/':
if (b === 0 && 1 / b === -Infinity) {
r = Infinity;
} else {
r = a / b;
}
break;
default:
console.log(`Sorry, wrong operator: ${operation}.`);
}
console.log(JSON.stringify(`value of c: ${c}`)); // "value of c: 0.30000000000000004"
let f = +c.toFixed(1);
let e = +d.toFixed(1);
console.log(JSON.stringify(`value of f: ${f}`)); // "value of f: 0.3"
console.log(typeof f); //number
console.log(JSON.stringify(`value of d: ${d}`)); // "value of d: NaN"
console.log(typeof d); //number
console.log(JSON.stringify(`value of e: ${e}`)); // "value of e: NaN"
console.log(typeof e); //number
if (f !== e) return false;
// if (!Object.is(f, e)) return false;
return true;
}
console.log(calc('0.1', '0.2', '+', '0.3'));
最佳答案
您可以创建一个函数来测试两个数字是否足够接近以至于可以称为相等,而不是在字符串之间来回转换。您决定了一些小的增量,如果数字至少接近那么大,您就认为它很好。
function almost(a, b, delta = 0.000001){
return Math.abs(a - b) < delta
}
// not really equal
console.log("equal?", 0.2 + 0.1 === 0.3)
// but good enough
console.log("close enough?", almost(0.2 + 0.1, 0.3))
关于javascript - 在 JavaScript 中添加和比较两个十进制数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56178084/
我正在尝试编写一个程序,该程序接受十六进制、八进制和小数的输入,将它们存储在整数变量中,并将它们连同它们转换为十进制形式一起输出。例如: 用户输入:0x43、0123、65 程序输出: 0x43 he
var re = /^([0-9]*)(\.[0-9]{2})$/ re.test(.22) true re.test(.20) false re.test(10.02) true re.test(1
我有一个类型为BigDecimal的属性“initialPrice”的“Trade”类。该属性可以 根据另一个类别“Symbol”中包含的属性“decimals”,小数位数会有所不同,因此需要使用不同
我是这里编码的新手。 我正在尝试使用编码来显示平均值、最大值和最小值的统计信息。 我遇到了错误,他们说我无法将 decimal[] 转换为 int[]。我在我的代码旁边评论了下面的错误。这是最后几行之
在不久的将来,我一直在研究小数类型以获得一些可能的编程乐趣,并希望将它用作比 Int64 更大的整数。一个关键点是我需要找出我可以安全地存储为小数(不丢失精度)的最大整数;我这样说,因为显然它在那里使
所以我这样做了: for(i=1;i0;i--) { if(MASKBINARY[i-1]==1 && MASKBINARY[i]==0) // check if the next eleme
我正在尝试将数字四舍五入到最接近的指定小数。 我希望它四舍五入到小数点 .11、.22、.33、.44、.55、.66、.77、.88 但不是 .99。相反,0.99 应该是 1。 例如: 1.14
获取 python (python 3) Decimal 的整数部分和小数部分的最有效方法是什么? 这是我现在拥有的: from decimal import * >>> divmod(Decimal
我有一个使用十进制列表样式的 ol: ol li { list-style-type: decimal; } First Second 编号工作正常,但如何删除句点?而不是显示: 1.
将字节数组转换为具有所选基数的字符串的最佳方法是什么? S.O. 上有大量示例。和其他地方转换为十六进制字符串。我在这里主要感兴趣的是将十六进制或十进制字符串转换为其他;也是一种更通用的方式。 这是我
不清楚十进制类型的 sizeof。以字节为单位的大小是否像在 sql server 中一样因精度而异? c# 类型的精度变量是“十进制”吗? 我不想打开不安全的代码来只调用小数类型的 sizeof。你
我必须向十进制值添加尾随零。不仅用于显示(因此 Format 不是一个选项),而且用于实际的底层数据,因为小数精度在我们的应用程序中很重要。 我试过: decimal value = 1M decim
我正在使用以下代码行将 decimal 转换为 string: decimal a = 0; a.ToString(); Resharper 给了我以下警告:“明确指定字符串区域性”。我想这是有道理的
我注意到 .NET 在涉及小数和尾随零时有一些古怪/不直观的行为。 0m == 0.000m //true 0.1m == 0.1000m //true 但是 (0m).ToString() == (
最近写单片机 RTC 日期、时间配置,需要实现十进制、BCD码互换,将示例Demo分享给各位朋友: BCD是指用二进制来表示十进制数的编码,即:用4位二进制来表示一位十进制数,因此4位二进制数
这似乎是重复的,但我找不到合适的答案(问题足够接近但是..)我有一个代表十进制数的字符串,它总是有很多小数位,至少 20,有时最多 2000 (代表特定的验证计算,即像'是数字 135 到 147 素
我想知道我是否会收到这样的问题: "Convert a decimal number to two's complement, then give your answer in Hex". 下面是路径
此存储过程不返回十进制格式 00.00 的薪水 ALTER PROCEDURE taxable_varsalary @emp_code bigint, @co_id bigint AS de
使用 leaflet R 包,有没有办法将 zoom 设置为非整数(即十进制)值? 也就是说,介于这两个缩放级别之间: 例子 随着 zoom 从第 3 级移动到第 4 级,它看起来好像正在四舍五入到最
我如何编写一个函数来将十进制数字符串转换为十进制数并将十进制数转换为字符串? 最佳答案 有非常方便的 clojure 函数可以将任何内容转换为字符串以及将类似数字的内容转换为 BigDecimal:
我是一名优秀的程序员,十分优秀!