float 和 double 问题:
- 专为科学和数学计算而设计,执行二进制浮点运算。
- 不适合货币计算或需要精确答案的情况。
- 它们无法准确表示 10 的负幂,例如 0.1,这会导致错误。
示例1:
减去美元金额时计算错误:
System.out.println(1.03 - 0.42); // Resultado: 0.6100000000000001
示例2:
以每件 10 美分的价格购买 9 件商品时出错:
System.out.println(1.00 - 9 * 0.10); // Resultado: 0.09999999999999998
即使四舍五入,错误仍然存在。
累进计算存在问题,例如以 0.10 到 1.00 的增量价格购买糖果时。
示例 3:
没钱买糖果时犯的错误:
double funds = 1.00; for (double price = 0.10; funds >= price; price += 0.10) { funds -= price; } System.out.println(funds); // Resultado: 0.3999999999999999
解决方案 1:使用 BigDecimal
- 非常适合财务计算和需要精确度的情况。
- 避免使用 BigDecimal 的双构造函数,最好使用 String 构造函数。
BigDecimal 示例:
BigDecimal funds = new BigDecimal("1.00"); BigDecimal price = new BigDecimal("0.10"); int itemsBought = 0; while (funds.compareTo(price) >= 0) { funds = funds.subtract(price); price = price.add(new BigDecimal("0.10")); itemsBought++; } System.out.println(itemsBought + " items bought. Money left: " + funds); // Resultado: 4 items bought. Money left: 0.00
计算现在很精确。
BigDecimal 的缺点:
- 使用起来不如原始类型方便。
- 速度较慢,特别是对于大量操作。
解决方案 2:使用 int 或 long
- 为了避免准确性问题,请使用 int 或 long 而不是 double 来以美分为单位进行计算。
int 示例(以分为单位):
int funds = 100; // 1.00 dólar = 100 centavos int price = 10; // 0.10 dólar = 10 centavos int itemsBought = 0; while (funds >= price) { funds -= price; price += 10; itemsBought++; } System.out.println(itemsBought + " items bought. Money left: " + funds); // Resultado: 4 items bought. Money left: 0
计算又快又准。
结论:
- 不要使用 float 或 double 进行需要精确精度的计算。
- 使用 BigDecimal 进行货币计算或需要小数精度的情况。
- 使用 int 或 long 进行不涉及大数的金融计算,以分为单位进行计算。
选择:
- 对于最多 9 位数字,请使用 int。
- 对于最多 18 位数字,请使用 long。
- 对于较大数量,请使用 BigDecimal。
- 此摘要表明,根据上下文,在 BigDecimal、int 或 long 之间进行选择可以优化精度和性能。
以上是如果需要准确答案,请避免浮动和双精度的详细内容。更多信息请关注PHP中文网其他相关文章!

JavaachievesPlatFormIndependencethroughTheJavavIrtualMachine(JVM),允许Codetorunondifferentoperatingsystemsswithoutmodification.thejvmcompilesjavacodeintoplatform-interploplatform-interpectentbybyteentbytybyteentbybytecode,whatittheninternterninterpretsandectectececutesoneonthepecificos,atrafficteyos,Afferctinginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginging

JavaispoperfulduetoitsplatFormitiondence,对象与偏见,RichstandardLibrary,PerformanceCapabilities和StrongsecurityFeatures.1)Platform-dimplighandependectionceallowsenceallowsenceallowsenceallowsencationSapplicationStornanyDevicesupportingJava.2)

Java的顶级功能包括:1)面向对象编程,支持多态性,提升代码的灵活性和可维护性;2)异常处理机制,通过try-catch-finally块提高代码的鲁棒性;3)垃圾回收,简化内存管理;4)泛型,增强类型安全性;5)ambda表达式和函数式编程,使代码更简洁和表达性强;6)丰富的标准库,提供优化过的数据结构和算法。

javaisnotirelyPlatemententduetojvmvariationsandnativecodinteintration,butitlargelyupholdsitsitsworapromise.1)javacompilestobytecoderunbythejvm

thejavavirtualmachine(JVM)IsanabtractComputingmachinecrucialforjavaexecutionasitrunsjavabytecode,使“ writeononce,runanywhere”能力

Javaremainsagoodlanguageduetoitscontinuousevolutionandrobustecosystem.1)Lambdaexpressionsenhancecodereadabilityandenablefunctionalprogramming.2)Streamsallowforefficientdataprocessing,particularlywithlargedatasets.3)ThemodularsystemintroducedinJava9im

Javaisgreatduetoitsplatformindependence,robustOOPsupport,extensivelibraries,andstrongcommunity.1)PlatformindependenceviaJVMallowscodetorunonvariousplatforms.2)OOPfeatureslikeencapsulation,inheritance,andpolymorphismenablemodularandscalablecode.3)Rich

Java的五大特色是多态性、Lambda表达式、StreamsAPI、泛型和异常处理。1.多态性让不同类的对象可以作为共同基类的对象使用。2.Lambda表达式使代码更简洁,特别适合处理集合和流。3.StreamsAPI高效处理大数据集,支持声明式操作。4.泛型提供类型安全和重用性,编译时捕获类型错误。5.异常处理帮助优雅处理错误,编写可靠软件。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

Dreamweaver Mac版
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。