C语言 C程序对数字进行四舍五入,四舍五入到小数点后第二位
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21373034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C program to round numbers, rounded to second position after the decimal place
提问by pdhimal1
I am writing a simple program to round numbers. It works fine when I enter something like 1.4 (gives me 1.0) or 2.6 (gives me 3.0). But when I enter 1.45, it should give me 1.5 but it prints 1.0. Or for 2.85 (should be 2.9, it prints 3.0). How do I make this work? I cannot use any functions.
我正在编写一个简单的程序来舍入数字。当我输入 1.4(给我 1.0)或 2.6(给我 3.0)之类的东西时,它工作正常。但是当我输入 1.45 时,它应该给我 1.5 但它打印 1.0。或者对于 2.85(应该是 2.9,它打印 3.0)。我如何使这项工作?我无法使用任何功能。
#include<stdio.h>
const float add = 0.5;
int main(void)
{
float v;
printf("Please enter a value to be rounded: ");
scanf("%f", &v);
v = v + add;
v = (int)v;
printf("The rounded value is %.2f", v);
return 0;
}
回答by Fahad Naeem
Try this code. You need to break your floating value into Modulus and Dividend.
试试这个代码。您需要将浮动价值分解为模数和股息。
#include<stdio.h>
int main(void)
{
float v;
int con, r_value, mul;
printf("Please enter a value to be rounded: ");
scanf("%f", &v);
mul=v*10;
con=mul%10;
r_value=mul/10;
if(con>=5)
r_value=r_value+1;
else
r_value=r_value;
printf("The rounded value is %.2d", r_value);
return 0;
}
Above code is modified to be used for Negative Numbers also.
上面的代码被修改为也用于负数。
float v;
int con, r_value, mul;
printf("Please enter a value to be rounded: ");
scanf("%f", &v);
mul=v*10;
con=mul%10;
r_value=mul/10;
if(v>0)
{
if(con>=5)
r_value=r_value+1;
else
r_value=r_value;
}
else if (v<0)
{
if(con<=-5)
r_value=r_value-1;
else
r_value=r_value;
}
printf("The rounded value is %.2d", r_value);
回答by KARTHIK BHAT
Int value simply removes the value after decimal point it does't round of to nearest number
Int 值只是删除小数点后的值,它不会四舍五入到最接近的数字
In your case when you give 2.45
在你的情况下,当你给2.45
2.45 + 0.5 = 2.95
2.45 + 0.5 = 2.95
which is rounded off to 2(2.0 since your asking for precision) i.e the decimal part is truncated not rounded off.
它被四舍五入到 2(2.0,因为你要求精度),即小数部分被截断而不是四舍五入。
回答by IllusiveBrian
Ints do not have decimal places, so a cast from float to int then back to float will leave only the integer value.
Int 没有小数位,因此从 float 转换为 int 然后再转换回 float 将只留下整数值。
回答by hymie
Your subject (rounding to the second decimal place) does not match your question (rounding to an arbitrary position based on the input). That makes it difficult to answer your question properly.
您的主题(四舍五入到小数点后第二位)与您的问题不匹配(根据输入四舍五入到任意位置)。这使得很难正确回答您的问题。
That said, your code
也就是说,你的代码
v = v + 0.5;
v = (int)v;
will round a number to the nearest integer. I'm not sure why you expect the intcast to do anything else.
将数字四舍五入到最接近的整数。我不知道你为什么希望int演员做其他事情。
回答by γηρ?σκω δ' αε? πολλ? διδασκ?με
You can do:
你可以做:
printf("%.2f", ((int)(v * 100 + 0.5) / 100.0));
回答by Don't You Worry Child
You are using v = (int)v;, how can it give 1.5for input of 1.45.
你在用v = (int)v;,怎么可能给1.5输入的1.45。
To get what you want, you can just print the decimal digits 1 less than in original, i.e.
Suppose you have 2.455which has 3 decimal digits, you can just print that number using %.2fand it will automatically be rounded off.
为了得到你想要的,你可以只打印比原始数字少 1 的十进制数字,即假设你有2.4553 个十进制数字,你可以只打印那个数字%.2f,它会自动四舍五入。

