01-浮点数精度问题bug

浮点数精度问题: 用实际生产环境一个发现的BUG 为例,进行记录;

01-问题背景

合作方对价格字段 要求 .00 标准格式 ; 也就是2位小数点;
因此我方对接开发代码对价格字段专门写一个方法,目的就是输出 保留两位小数的字符串
方法明细:

package org.example;


import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * Author: kaka@bug.com
 * Date:   2023/12/8-18:40
 * ---------------------------------------
 * Desc:   输出保留两位小数的字符串
 */
public class Test {

    public static void main(String[] args) throws Exception {
        double p1 = 235.6;
        String pr1 = getBookPriceToString(p1);

        double p2 = 157.7;
        String pr2 = getBookPriceToString(p2);
        System.out.println(pr1); // 输出235.60
        System.out.println(pr2); // 输出157.7
    }

    // 总的来说,这段代码的目的是为了保证价格的字符串形式总是有两位小数,如果原始价格的小数部分不足两位,则会进行补"0"的操作。
    public static String getBookPriceToString(Double buyPrice) throws Exception {
        Double buPrice1 = buyPrice * 100;
        Integer roundNumber = buPrice1.intValue();
        String buyPriceStr = buyPrice.toString();
        if (roundNumber % 10 > 0) {
            return buyPriceStr;
        }
        if (roundNumber % 100 > 0) {
            return buyPriceStr + "0";
        }
        return buyPrice.intValue() + ".00";
    }


    /*
    * 这段代码的主要功能是将传入的buyPrice(购买价格)转换为字符串格式,并根据其小数部分的情况进行不同的处理。

Double buPrice1 = buyPrice * 100;:将buyPrice乘以100,目的是为了后续判断价格的小数部分是否存在。

Integer roundNumber = buPrice1.intValue();:将乘以100后的价格转换为整数,这样就可以通过取余的方式来判断小数部分的情况。

String buyPriceStr = buyPrice.toString();:将buyPrice转换为字符串,为后续的返回做准备。

if (roundNumber % 10 > 0) { return buyPriceStr; }:如果价格的小数部分的十分位大于0(即价格的小数部分不是0.X0的形式),则直接返回原始价格的字符串形式。

if (roundNumber % 100 > 0) { return buyPriceStr + "0"; }:如果价格的小数部分的百分位大于0(即价格的小数部分是0.0X的形式),则在原始价格的字符串形式后面加上"0"。

return buyPrice.intValue() + ".00";:如果价格的小数部分为0(即价格为整数),则在价格后面加上".00"。

总的来说,这段代码的目的是为了保证价格的字符串形式总是有两位小数,如果原始价格的小数部分不足两位,则会进行补"0"的操作。
    * */

}

当输入157.7时候, debug运行计算时候的过程如下图:
61635f0b8985a5368478b195a13b36d.jpg

02-改善方法

package org.example;


import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * Author: method1
 * Date:   2023/12/8-18:40
 * ---------------------------------------
 * Desc:   描述该类的作用
 */
public class Test {

    public static void main(String[] args) throws Exception {
        double p1 = 235.6;
        String pr1 = getBookPriceToString(p1);

        double p2 = 157.7;
        String pr2 = getBookPriceToString(p2);
        System.out.println(pr1); // 输出235.60
        System.out.println(pr2); // 输出157.7

        System.out.println(getBookPriceToStringNew(235.6)); // 输出235.60
        System.out.println(getBookPriceToStringNew(157.7)); // 输出157.70
    }

    // 总的来说,这段代码的目的是为了保证价格的字符串形式总是有两位小数,如果原始价格的小数部分不足两位,则会进行补"0"的操作。
    public static String getBookPriceToString(Double buyPrice) throws Exception {
        Double buPrice1 = buyPrice * 100;
        Integer roundNumber = buPrice1.intValue();
        String buyPriceStr = buyPrice.toString();
        if (roundNumber % 10 > 0) {
            return buyPriceStr;
        }
        if (roundNumber % 100 > 0) {
            return buyPriceStr + "0";
        }
        return buyPrice.intValue() + ".00";
    }

    // 优化后 方法的目的应该是:输出保留两位小数的字符串
    public static String getBookPriceToStringNew(Double buyPrice) throws Exception {
        DecimalFormat df = new DecimalFormat("0.00");
        return df.format(buyPrice);
    }
}
package org.example;


import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * Author: method2
 * Date:   2023/12/8-18:40
 * ---------------------------------------
 * Desc:   
 */
public class Test {

    public static void main(String[] args) throws Exception {
        double p1 = 235.6;
        String pr1 = getBookPriceToString(p1);

        double p2 = 157.7;
        String pr2 = getBookPriceToString(p2);
        System.out.println(pr1); // 输出235.60
        System.out.println(pr2); // 输出157.7

        System.out.println(getBookPriceToStringNew(235.6)); // 输出235.60
        System.out.println(getBookPriceToStringNew(157.7)); // 输出157.70

        System.out.println(getBookPriceToString2(157.7)); // 输出157.70
        System.out.println(getBookPriceToString3(157.7)); // 157.7

    }

    // 目的是为了保证价格的字符串形式总有两位小数,如果原始价格的小数部分不足两位,则会进行补"0"的操作。
    public static String getBookPriceToString(Double buyPrice) throws Exception {
        Double buPrice1 = buyPrice * 100;
        Integer roundNumber = buPrice1.intValue();
        String buyPriceStr = buyPrice.toString();
        if (roundNumber % 10 > 0) {
            return buyPriceStr;
        }
        if (roundNumber % 100 > 0) {
            return buyPriceStr + "0";
        }
        return buyPrice.intValue() + ".00";
    }

    public static  String getBookPriceToString2(Double buyPrice) throws Exception {
        if (buyPrice == null) {
            throw new Exception("buyPrice cannot be null");
        }
        if (buyPrice <= 0) {
            throw new Exception("buyPrice must be greater than 0");
        }
        DecimalFormat df = new DecimalFormat("#.00");
        return df.format(buyPrice);
    }

    // 优化后 方法的目的应该是:输出保留两位小数的字符串
    public static String getBookPriceToStringNew(Double buyPrice) throws Exception {
        DecimalFormat df = new DecimalFormat("0.00");
        return df.format(buyPrice);
    }

    // 实际上线代码
    public static String getBookPriceToString3(Double buyPrice) throws Exception {
        BigDecimal price = BigDecimal.valueOf(buyPrice);
        DecimalFormat df = new DecimalFormat("#.00");
        return df.format(price);
    }
}

综上所述

浮点数精度问题 可以通过 BigDecimal 或者 DecimalFormat 来解决;

测试驱动的编程是 XP 困扰程序员的一个方面。对于测试驱动的编程意味着什么以及如何去做,大多数人都做出了不正确的假设。这个月,XP 方面的讲师兼 Java 开发人员 Roy Miller 谈论了测试驱动的编程是什么,它为什么可以使程序员的生产力和质量发生巨大变化,以及编写测试的原理。请在与本文相随的 论坛中提出您就本文的想法,以飨笔者和其他读者。(您也可以单击本文顶部或底部的“讨论”来访问该论坛。) 最近 50 年来,测试一直被视为项目结束时要做的事。当然,可以在项目进行之中结合测试测试通常并不是在 所有编码工作结束后才开始,而是一般在稍后阶段进行测试。然而,XP 的提倡者建议完全逆转这个模型。作为一名程序员,应该在编写代码 之前编写测试,然后只编写足以让测试通过的代码即可。这样做将有助于使您的系统尽可能的简单。 先编写测试 XP 涉及两种测试: 程序员测试和 客户测试测试驱动的编程(也称为 测试为先编程)最常指第一种测试,至少我使用这个术语时是这样。测试驱动的编程是让 程序员测试(即单元测试 ― 重申一下,只是换用一个术语)决定您所编写的代码。这意味着您必须在编写代码之前进行测试测试指出您 需要编写的代码,从而也 决定了您要编写的代码。您只需编写足够通过测试的代码即可 ― 不用多,也不用少。XP 规则很简单:如果不进行程序员测试,则您不知道要编写什么代码,所以您不会去编写任何代码。 测试驱动开发(TDD)是极限编程的重要特点,它以不断的测试推动代码的开发,既简化了代码,又保证了软件质量。本文从开发人员使用的角度,介绍了 TDD 优势、原理、过程、原则、测试技术、Tips 等方面。 背景 一个高效的软件开发过程对软件开发人员来说是至关重要的,决定着开发是痛苦的挣扎,还是不断进步的喜悦。国人对软件蓝领的不屑,对繁琐冗长的传统开发过程的不耐,使大多数开发人员无所适从。最近兴起的一些软件开发过程相关的技术,提供一些比较高效、实用的软件过程开发方法。其中比较基础、关键的一个技术就是测试驱动开发(Test-Driven Development)。虽然TDD光大于极限编程,但测试驱动开发完全可以单独应用。下面就从开发人员使用的角度进行介绍,使开发人员用最少的代价尽快理解、掌握、应用这种技术。下面分优势,原理,过程,原则,测试技术,Tips等方面进行讨论。 1. 优势 TDD的基本思路就是通过测试来推动整个开发的进行。而测试驱动开发技术并不只是单纯的测试工作。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值