C语言如何格式化小数点后的两位数字?(复制)

时间:2022-02-28 17:07:40

Possible Duplicate:
Limit floating point precision?

可能重复:限制浮点精度?

In C language, I need to format a number to 2 digits after the decimal point from user input

在C语言中,我需要在用户输入小数点后将数字格式化为两位数

For example:

例如:

 float x ; 
 printf("Enter number");

Let's suppose the user enters 4.54234635

假设用户输入4.54234635

I need to print and process in the whole program: 4.54

我需要打印并处理整个程序:4.54

Thanks advance

谢谢提前

3 个解决方案

#1


11  

scanf("%.2f",&x);

And i think that will solve a problem

我认为这将解决一个问题

#2


6  

The complete list

的完整列表

Integer   display
%d      print as decimal integer
%6d     print as decimal integer, at least 6 characters wide
%f      print as floating point
%6f     print as floating point, at least 6 characters wide
%.2f    print as floating point, 2 characters after decimal point

With the required modification (scanf("%.2f",&x); in the last entry) will solve your problem

有必要的修改(scanf(“%.2f”,&x);在最后一项中)将解决你的问题。

#3


1  

use

使用

scanf("%.2f",&number);

or
printf("%.2f", number);

或printf(" %。2 f”,编号);

#1


11  

scanf("%.2f",&x);

And i think that will solve a problem

我认为这将解决一个问题

#2


6  

The complete list

的完整列表

Integer   display
%d      print as decimal integer
%6d     print as decimal integer, at least 6 characters wide
%f      print as floating point
%6f     print as floating point, at least 6 characters wide
%.2f    print as floating point, 2 characters after decimal point

With the required modification (scanf("%.2f",&x); in the last entry) will solve your problem

有必要的修改(scanf(“%.2f”,&x);在最后一项中)将解决你的问题。

#3


1  

use

使用

scanf("%.2f",&number);

or
printf("%.2f", number);

或printf(" %。2 f”,编号);