zoukankan      html  css  js  c++  java
  • Java导出防止小数显示不全工具类

    1、说明

         在做项目的过程中,发现导出功能中的数据显示不全,如“0.4”。会显示成“.4”。“-0.8”会显示成“-.8”

         如今。通过下面Java工具类保证导出的数据(特别是小数)显示全


    2、Java工具类

    /**
     * @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 + "
    " + "负小数:" + resultTwo);
    	}
    
    }
    

    3、实现结果

    正小数:0.8
    负小数:-0.7


  • 相关阅读:
    hdu 2222 Keywords Search
    Meet and Greet
    hdu 4673
    hdu 4768
    hdu 4747 Mex
    uva 1513 Movie collection
    uva 12299 RMQ with Shifts
    uva 11732 strcmp() Anyone?
    uva 1401
    hdu 1251 统计难题
  • 原文地址:https://www.cnblogs.com/yjbjingcha/p/6814497.html
Copyright © 2011-2022 走看看