自己写的java小数点处理程序

import java.text.DecimalFormat;
/**
 *
 * @author Michael
 * 本类实现了在类似于金额计算方面对小数点的处理方式
 * java自带的DecimalFormat类也可以处理小数点的问题
 * 此例是专门用于拓宽数组使用 String类中方法使用思路
 * 而用的,希望大家喜欢和指出不足

 * E-mail: zhaodongbo07@sohu.com
 */
public class Math {
 
 /**存储最终处理完成的小数的静态成员变量*/
 private static String price;
 
 /**
  * 获取处理后的结果
  * @return price 最终返回处理完的小数形式
  */
 public static String getPrice() {
  return price;
 }
 
 /**
  * 此方法是处理数字的主要方法
  * @param price 需要进行处理的数字
  */
 public static void setPrice(String price) {

  // 先判断此此字符串变量包含的数值数是否为带小数点的形式
  if (price.indexOf(".") != -1) {

   // 以小数点的部分将数据截取为整数部分和小数部分两部分分别处理两部分数据
   String[] str = price.split("//.");
   
   //小数点位数在三位以上的情况的处理方式
   if (str[1].length() > 2) {
    
    //包含的第三位小数为大于等于5的情况,这时候需要考虑四舍五入
    if (Integer.parseInt(str[1].substring(2, 3)) > 4) {
     str[1] = Integer.parseInt(str[1].substring(0, 2)) + 1 + "";
     if (str[1].length() < 2) {
      str[1] = "0" + str[1];
     }
    }
    //小数点的第三位比5小则直接截取
    else {
     str[1] = str[1].substring(0, 2);
    }
   }
   //如果小数的位数只有一位则在其后补0
   else if (str[1].length() < 2) {
    str[1] = str[1] + "0";
   }
   //当小数点的两位部分四舍五入后增大到100,这时候需要整数部分要进行加1进位
   if (str[1].equals("100")) {
    str[1] = "00";
    str[0] = Integer.parseInt(str[0]) + 1 + "";
   }

   //如果数字位数多想,进行数据分割时可以打开以下的注释部分 例如 999,888,666.00; 如果不需要此形式的话可以不使用以下判断
   
   //整数部分的长度
   int strLength = str[0].length();
   
   //整数部分刚好为3的整数倍数的情况
   if (strLength > 3 && strLength % 3 == 0) {
    String temp = "";

    for (int i = 0; i < strLength / 3; i++) {
     temp = temp + str[0].substring(0, 3) + ",";
     str[0] = str[0].substring(3);
    }
    str[0] = temp.substring(0, temp.length() - 1);

   }
   //整数部分和3取余数余数为2的情况
   else if (strLength > 3 && strLength % 3 == 2) {
    strLength = strLength - 2;
    String temp = str[0].substring(0, 2) + ",";
    str[0] = str[0].substring(2);
    for (int i = 0; i < strLength / 3; i++) {
     temp = temp + str[0].substring(0, 3) + ",";
     str[0] = str[0].substring(3);
    }
    str[0] = temp.substring(0, temp.length() - 1);
   }
   //整数部分和3取余数余数为1的情况
   else if (strLength > 3 && strLength % 3 == 1) {
    strLength = strLength - 1;
    String temp = str[0].substring(0, 1) + ",";
    str[0] = str[0].substring(1);
    for (int i = 0; i < strLength / 3; i++) {
     temp = temp + str[0].substring(0, 3) + ",";
     str[0] = str[0].substring(3);
    }
    str[0] = temp.substring(0, temp.length() - 1);
   }
   price = str[0] + "." + str[1];
  }

  // 不带小数点的字符串形式我们拼接成带两位小数点的形式显示
  else {
   price = price + ".00";
  }
  Math.price = price;
 }

 public static void main(String[] args) {
  String price = "10.4";
  Math.setPrice(price);
  System.out.println(Math.getPrice());
  

  //以上的最终操作相当于 以下的java自带的对小数点类处理的类的效果相同
//  DecimalFormat formater = new DecimalFormat();
//  formater.setMaximumFractionDigits(2);
//  formater.setMinimumFractionDigits(2);
//  System.out.println(formater.format(Double.parseDouble(price)));  
  
 }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值