相关疑难解决方法(0)

将浮点数舍入到预定义点的常规网格

我想将浮点数舍入到给定的精度,例如:

0.051 i want to convert it to
0.1

0.049 i want to convert it to
0.0

0.56 i want to convert it to
0.6

0.54 i want to convert it to
0.5
Run Code Online (Sandbox Code Playgroud)

我无法更好地解释它,但其原因是将点位置(如0.131f,0.432f)转换为网格中瓷砖的位置(如0.1f,0.4f).

c++ rounding

27
推荐指数
4
解决办法
6万
查看次数

舍入到2个小数点

我使用以下内容将我的值舍入为2小数点:

 x = floor(num*100+0.5)/100;
Run Code Online (Sandbox Code Playgroud)

这似乎工作正常; 除了"16.60"之类的值,即"16.6".

我想输出这个值,如"16.60".

我输出值的方式如下:

cout setw(12) << round(payment);
Run Code Online (Sandbox Code Playgroud)

我尝试过以下方法:

cout setw(12) << setprecision(2) << round(payment);
Run Code Online (Sandbox Code Playgroud)

但这给了我一些答案

1.2e+02
Run Code Online (Sandbox Code Playgroud)

如何正确输出值?

c++ double decimal

6
推荐指数
2
解决办法
5万
查看次数

标签 统计

c++ ×2

decimal ×1

double ×1

rounding ×1