1、说明
在做项目的过程中,发现导出功能中的数据显示不全,如“0.4”,会显示成“.4”;“-0.8”会显示成“-.8”
现在,通过以下Java工具类保证导出的数据(特别是小数)显示全
2、Java工具类
3、实现结果
- /**
- * @Title:DecimalPoint.java
- * @Package:com.you.model
- * @Description:解决导出时小数前的“0”被去掉的问题
- * @Author: 游海东
- * @date: 2014年7月8日 下午9:13:52
- * @Version V1.2.3
- */
- package com.you.model;
- /**
- * @类名:DecimalPoint
- * @描述:
- * @Author:
- * @date: 2014年7月8日 下午9:13:52
- */
- public class DecimalPoint
- {
- /**
- *
- * @Title : findPoint
- * @Type : DecimalPoint
- * @date : 2014年7月8日 下午9:16:20
- * @Description :
- * @param number
- * @return
- */
- public static String findPoint(String number)
- {
- //当参数为空时,返回""
- if(null == number)
- {
- return "";
- }
- //防止出现“.6”
- if(number.startsWith("."))
- {
- number = "0" + number;
- }
- //防止出现“-.6”
- else if(number.startsWith("-."))
- {
- number = "-0" + number.substring(1,number.length());
- }
- return number;
- }
- /**
- * @Title : main
- * @Type : DecimalPoint
- * @date : 2014年7月8日 下午9:13:55
- * @Description :
- * @param args
- */
- public static void main(String[] args)
- {
- //传“.8”
- String numOne = ".8";
- //传“-.7”
- String numTwo = "-.7";
- String resultOne = findPoint(numOne);
- String resultTwo = findPoint(numTwo);
- System.out.println("正小数:" + resultOne + "\n" + "负小数:" + resultTwo);
- }
- }
来自:
- 正小数:0.8
- 负小数:-0.7
来源: http://www.phpxs.com/code/1002684/