var figure = 0.0099999999999909;
alert(figure.toFixed(2));Run Code Online (Sandbox Code Playgroud)
我读过这篇文章,但我仍然被困住了.
有没有办法使用jQuery/Javascript将0.0099999999999909舍入到0.01?
我在代码片段上的示例实际上有效,但它不在我的实际代码中;
// allocate button
$( "#allocate_total_amount_paid" ).click(function() {
var totalAmountPaid = parseFloat($("#total_amount_paid").val());
$( ".amount_received" ).each(function( index ) {
var thisAmount = $(this).attr("max");
if (thisAmount <= totalAmountPaid) {
// If we have enough for this payment, pay it in full
$(this).val(thisAmount).trigger('input');
// and then subtract from the total payment
totalAmountPaid -= thisAmount;
} else {
// We don't have enough, so just pay what we have available
$(this).val(totalAmountPaid).trigger('input');
// Now we have …Run Code Online (Sandbox Code Playgroud) #include<stdio.h>
void main()
{
float a = 2.3;
if(a == 2.3) {
pritnf("hello");
}
else {
printf("hi");
}
}
Run Code Online (Sandbox Code Playgroud)
它在输出中打印"hi",或者我们可以说如果条件得到假值.
#include<stdio.h>
void main()
{
float a = 2.5;
if(a == 2.5)
printf("Hello");
else
printf("Hi");
}
Run Code Online (Sandbox Code Playgroud)
打印你好.
我正在处理非常精确的数字(最大位数).
我注意到write.csv(x)在R中有时围绕数字.
有没有人注意到这样的事情?保存的默认位数是多少?
我已经实现了这个功能:
double heron(double a)
{
double x = (a + 1) / 2;
while (x * x - a > 0.000001) {
x = 0.5 * (x + a / x);
}
return x;
}
Run Code Online (Sandbox Code Playgroud)
该功能按预期工作,但是我希望对其进行改进。它应该使用不间断的while循环来检查是否类似于x * xis a。a是用户应输入的数字。
到目前为止,我没有使用该方法的工作功能...这是我惨败的尝试:
double heron(double a)
{
double x = (a + 1) / 2;
while (x * x != a) {
x = 0.5 * (x + a / x);
}
return x;
}
Run Code Online (Sandbox Code Playgroud)
这是我的第一篇文章,因此,如果有任何不清楚或需要补充的内容,请告诉我。
尝试失败次数2:
double …Run Code Online (Sandbox Code Playgroud) 根据为什么浮点数不准确?,浮点数有错误.
我的问题是,对于正整数n,Math.random()*n是否有错误使其结果等于或大于n?
在阅读了这个问题以及这个msdn博客之后,我尝试了一些示例来对此进行测试:
Console.WriteLine(0.8-0.7 == 0.1);
Run Code Online (Sandbox Code Playgroud)
是的,预期输出为False。因此,我尝试将表达式的两端都强制转换为double,float以查看是否可以获得不同的结果:
Console.WriteLine((float)(0.8-0.7) == (float)(0.1));
Console.WriteLine((double)(0.8-0.7) == (double)(0.1));
Run Code Online (Sandbox Code Playgroud)
第一行输出True但第二行输出False,为什么会发生这种情况?
此外,
Console.WriteLine(8-0.7 == 7.3);
Console.WriteLine(8.0-0.7 == 7.3);
Run Code Online (Sandbox Code Playgroud)
上面的两行都给出True甚至不进行强制转换。还有...
Console.WriteLine(18.01-0.7 == 17.31);
Run Code Online (Sandbox Code Playgroud)
此行输出False。如果将两者都用浮点数相减,则如何从减法18.01中减去8的差?
我试图阅读博客和问题,但似乎找不到其他答案。有人可以向我解释为什么所有这些都是以莱曼的语言发生的吗?先感谢您。
编辑:
Console.WriteLine(8.001-0.001 == 8); //this return false
Console.WriteLine(8.01-0.01 == 8); //this return true
Run Code Online (Sandbox Code Playgroud)
注意:我正在使用.NET小提琴在线c#编译器。
我正在尝试编写一个函数的 C 代码,该函数接受一个整数从0to125并仅当它是整数( 1,2,3,4,5 )时才返回该整数的立方根,如果不是整数,0则返回。所以我写了这段代码:
unsigned int cubic(unsigned int n) {
if (n > 125)
return 0;
double root = pow(n, (1 / 3.));
double rem = (double)(roundf(root)) - root;
if (rem != 0)
return 0;
else
return roundf(root);
}
Run Code Online (Sandbox Code Playgroud)
此函数适用于除 number64和之外的所有情况125。在这些情况下,它返回的0不是这些数字的立方根,分别是4和5。有人可以向我解释为什么会发生这种情况吗?
我float和之间的区别感到困惑double.我读过这篇文章.我认为差异只在于精确度.所以我期望如果0.1 + 0.2 == 0.3返回false那么0.1f + 0.2f == 0.3f.
但实际上是0.1f + 0.2f == 0.3f回归true.为什么?
这是纯粹的Java问题还是什么?
#include <stdio.h>
#include <float.h>
#include <stdlib.h>
int main() {
float x = 3.8, y = 5.2;
int a, b, c;
float d = x + y;
a = d;
b = (int)(x + y);
c = (int)(3.8 + 5.2);
printf("a=%d,b=%d,c=%d\n", a, b, c);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
结果是
Run Code Online (Sandbox Code Playgroud)9,8,9
我认为9,9,9是正确的输出,请告诉我,为什么.
我试图满足这个问题:编写一个函数 print_dig_float(float f) 来打印浮点数 f 的每个数字的值。例如,如果 f 是 2345.1234,则 print_dig_float(f) 将连续打印数字 2、3、4、5、1、2、3 和 4 的整数值。
我所做的是:给定一个带小数的数字,我尝试将数字向左移动(例如:3.45 -> 345),将其乘以 10。之后,我通过取余数将每个数字存储在一个数组中,然后把它放在一个元素中。然后,我将它们打印出来。
所以我的程序是这样的:
#include <stdio.h>
void print_dig_float(float f);
int main(int argc, char const *argv[]) {
print_dig_float(23432.214122);
return 0;
}
void print_dig_float(float f) {
printf("%f\n", f);
int i = 0, arr[50], conv;
//move digits to the left
do {
f = f * 10;
conv = f;
printf("%i\n", conv);
} while (conv % 10 != 0);
conv = f / 10;
//store digits in …Run Code Online (Sandbox Code Playgroud)