仅用基本类型与String完成除法,不能整除向上保留两位小数

如0.125 ==> 0.13再如 0.1==>0.10

涉及ASCII码转换 整型 char计算


import lombok.extern.slf4j.Slf4j;


@Slf4j
public class TestIntDivide2Float {

    public static void main(String[] args) {
        double temp = 0.256983D;
        String postfix = String.valueOf(temp);
        if (postfix.charAt(4) >= '5') {
            // 0.126
            int t = postfix.charAt(3);
            postfix = postfix.substring(0, 3);
            char ch = (char) ((char) t + 1);
            log.error("结果: {}", postfix + String.valueOf(ch));
        }
        String ret = solution(1133L, 5555L);
        System.err.println("ret: " + ret);
    }

    public static String solution(long num, long divider) {
        double a0 = num;
        double b0 = divider;
        double temp0 = a0 / b0;
        System.err.println("整型相除结果:" + temp0);
        if (num < divider) {
            double a = num;
            double b = divider;
            double temp = a / b;
            String postfix = String.valueOf(temp);
            if (postfix.length() > 4) {
                char ch = postfix.charAt(4) > '4' ? (char)(postfix.charAt(3) + 1) : '0';
                if (ch != '0') {
                    char tempCh = (char)(postfix.charAt(3) + 1);
                    return postfix.substring(0, 3) + String.valueOf(tempCh);
                }
                return postfix.substring(0, 4);
            }
            if (postfix.length() == 4) {
                postfix = postfix.substring(0, 4);
                return postfix;
            }

            if (postfix.length() == 3) {
                postfix = postfix.substring(0, 3);
                return postfix + "0";
            }
        }
        long longNum = num / divider;
        long remainder = num % divider;
        String prefix = String.valueOf(longNum);
        if (remainder == 0L) {
            return prefix + ".00";
        } else {
            double a = remainder;
            double b = divider;
            double temp = a / b;

            // "-0.125"
            String postfix = String.valueOf(temp);
            log.warn("double <==> {}", temp);
            if (postfix.contains("-") && postfix.length() > 4) {
                if (postfix.charAt(4) >= '5') {
                    char t = postfix.charAt(3);
                    postfix = postfix.substring(0, 3);
                    int asciiChar = t - 0;
                    int intChar = (int) t;
                    char ch = (char) (t + 1);
                    log.error("减去整数0 <==> {} ,字符直接转整型 <==> {}", asciiChar, intChar);
                    return prefix + postfix + String.valueOf(ch);
                }
            }
            if (postfix.contains("-") && postfix.length() <= 4) {
                if (postfix.length() == 4) {
                    postfix = postfix.substring(1, 4);
                    return prefix + postfix;
                }
                if (postfix.length() == 3) {
                    postfix = postfix.substring(1, 3);
                    return prefix + postfix + "0";
                }
            }
            if (!postfix.contains("-") && postfix.length() > 5) {
                if (postfix.charAt(4) >= '5') {
                    // 0.126
                    int t = postfix.charAt(3);
                    postfix = postfix.substring(0, 3);
                    char ch = (char) ((char) t + 1);
                    return prefix + postfix + String.valueOf(ch);
                }
            } else {
                if (postfix.length() == 4) {
                    postfix = postfix.substring(0, 4);
                    return prefix + postfix;
                }

                if (postfix.length() == 3) {
                    postfix = postfix.substring(0, 3);
                    return prefix + postfix + "0";
                }
            }
        }
        return "";
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值