更改数字精度 [英] Changing number precision

查看:36
本文介绍了更改数字精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何更改数字的精度,例如: float n = 1.2345 并将其更改为 1.23 并将其存储回变量'n'中?/p>

How would you change the precision of a number for example: float n = 1.2345 and store it back to the variable 'n' with changing it to 1.23 ?

推荐答案

float n = 1.2345;
int scaled = n * 100
n = static_cast<float>(scaled)/100.0;

或一行:

n = static_cast<float>( static_cast<int>(n*100) ) / 100;

这篇关于更改数字精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆