printf%f小数点后只有2个数字?

时间:2021-09-02 17:16:37

In my printf, I need to use %f but I'm not sure how to truncate to 2 decimal places:

在我的printf中,我需要使用%f,但我不知道如何截断到2位小数:

Example: getting

示例:获取

3.14159

3.14159

to print as:

打印为:

3.14

3.14

8 个解决方案

#1


72  

Use this:

用这个:

printf ("%.2f", 3.14159);

#2


12  

You can use something like this:

你可以使用这样的东西:

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

If you need to use the string for something other than printing out, use the NumberFormat class:

如果您需要将字符串用于打印输出以外的其他内容,请使用NumberFormat类:

NumberFormat formatter = new DecimalFormatter("#.##");
String s = formatter.format(3.14159265); // Creates a string containing "3.14"

#3


3  

Try:

尝试:

printf("%.2f", 3.14159);

printf(“%。2f”,3.14159);

Reference:

参考:

http://www.cplusplus.com/reference/clibrary/cstdio/printf/

http://www.cplusplus.com/reference/clibrary/cstdio/printf/

#4


3  

as described in Formatter class, you need to declare precision. %.2f in your case.

如Formatter类中所述,您需要声明精度。在你的情况下%.2f。

#5


3  

Use this

用这个

printf ("%.2f", 3.14159);

#6


3  

System.out.printf("%.2f", number);

BUT, this will round the number to the nearest decimal point you have mentioned.(As in your case you will get 3.14 since rounding 3.14159 to 2 decimal points will be 3.14)

但是,这会将数字四舍五入到你提到的最接近的小数点。(在你的情况下,你将得到3.14,因为舍入3.14159到2小数点将是3.14)

Since the function printf will round the numbers the answers for some other numbers may look like this,

由于函数printf会对数字进行舍入,因此其他一些数字的答案可能如下所示:

System.out.printf("%.2f", 3.14136); -> 3.14
System.out.printf("%.2f", 3.14536); -> 3.15
System.out.printf("%.2f", 3.14836); -> 3.15

If you just need to cutoff the decimal numbers and limit it to a k decimal numbers without rounding,

如果你只需要截断十进制数并将其限制为k个十进制数而不进行舍入,

lets say k = 2.

比方说k = 2。

System.out.printf("%.2f", 3.14136 - 0.005); -> 3.14
System.out.printf("%.2f", 3.14536 - 0.005); -> 3.14
System.out.printf("%.2f", 3.14836 - 0.005); -> 3.14

#7


1  

You can try printf("%.2f", [double]);

你可以尝试printf(“%。2f”,[double]);

#8


0  

I suggest to learn it with printf because for many cases this will be sufficient for your needs and you won't need to create other objects.

我建议用printf学习它,因为在很多情况下,这将足以满足您的需求,您不需要创建其他对象。

double d = 3.14159;     
printf ("%.2f", d);

But if you need rounding please refer to this post

但如果你需要四舍五入,请参阅这篇文章

https://*.com/a/153785/2815227

https://*.com/a/153785/2815227

#1


72  

Use this:

用这个:

printf ("%.2f", 3.14159);

#2


12  

You can use something like this:

你可以使用这样的东西:

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

If you need to use the string for something other than printing out, use the NumberFormat class:

如果您需要将字符串用于打印输出以外的其他内容,请使用NumberFormat类:

NumberFormat formatter = new DecimalFormatter("#.##");
String s = formatter.format(3.14159265); // Creates a string containing "3.14"

#3


3  

Try:

尝试:

printf("%.2f", 3.14159);

printf(“%。2f”,3.14159);

Reference:

参考:

http://www.cplusplus.com/reference/clibrary/cstdio/printf/

http://www.cplusplus.com/reference/clibrary/cstdio/printf/

#4


3  

as described in Formatter class, you need to declare precision. %.2f in your case.

如Formatter类中所述,您需要声明精度。在你的情况下%.2f。

#5


3  

Use this

用这个

printf ("%.2f", 3.14159);

#6


3  

System.out.printf("%.2f", number);

BUT, this will round the number to the nearest decimal point you have mentioned.(As in your case you will get 3.14 since rounding 3.14159 to 2 decimal points will be 3.14)

但是,这会将数字四舍五入到你提到的最接近的小数点。(在你的情况下,你将得到3.14,因为舍入3.14159到2小数点将是3.14)

Since the function printf will round the numbers the answers for some other numbers may look like this,

由于函数printf会对数字进行舍入,因此其他一些数字的答案可能如下所示:

System.out.printf("%.2f", 3.14136); -> 3.14
System.out.printf("%.2f", 3.14536); -> 3.15
System.out.printf("%.2f", 3.14836); -> 3.15

If you just need to cutoff the decimal numbers and limit it to a k decimal numbers without rounding,

如果你只需要截断十进制数并将其限制为k个十进制数而不进行舍入,

lets say k = 2.

比方说k = 2。

System.out.printf("%.2f", 3.14136 - 0.005); -> 3.14
System.out.printf("%.2f", 3.14536 - 0.005); -> 3.14
System.out.printf("%.2f", 3.14836 - 0.005); -> 3.14

#7


1  

You can try printf("%.2f", [double]);

你可以尝试printf(“%。2f”,[double]);

#8


0  

I suggest to learn it with printf because for many cases this will be sufficient for your needs and you won't need to create other objects.

我建议用printf学习它,因为在很多情况下,这将足以满足您的需求,您不需要创建其他对象。

double d = 3.14159;     
printf ("%.2f", d);

But if you need rounding please refer to this post

但如果你需要四舍五入,请参阅这篇文章

https://*.com/a/153785/2815227

https://*.com/a/153785/2815227