c++中,将float/double转换为int时损失1
C++, losing 1 when converting float/double to int?
我想计算零钱,并将其转换为便士。
#include <iostream>
using namespace std;
int main()
{
float cost = 5.15,
paid = 10.00,
change = 0.0;
int pennies = 0.0;
change = paid - cost;
pennies = static_cast<int>(change * 100);
cout << change << endl; //4.85
cout << pennies << endl; //484 ??
return 0;
}
penny的计算结果是484,我的penny去哪了?
我已经尝试过float和double,有和没有static_cast。
我在c++入门课上,所以重点是用基本的操作来做这些。
剂量(4.85 * 100)评价为484.999999…当使用浮点数时,它被截断了吗?
使用 .........................................................
相关文章:
- 为什么在全局范围内使用"extern int a"似乎不行?
- int(c) 和 c-'0' 之间的区别。C++
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 为什么野牛仍在使用"int yylex(void)",却找不到"int yylex(YYS
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 是否可以从int转换为enum类类型
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- 'short int'持有的值溢出,但"自动"不会溢出?
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- 调用'begin(int [n])'没有匹配函数
- 没有显式声明的int[]中的foreach
- 在c++中访问int到类对象的映射时出错
- 为什么我无法更改"set<set>"循环中的值<int>
- 长 长 int 不要 长 int 好
- int 到浮点转换的精度损失
- 将非常大的 int 转换为双倍,在某些计算机上会损失精度
- c++中,将float/double转换为int时损失1
- 从Int到Float到Int的转换精度损失