• Java实现数字的千分位的处理


    前言:

    最近在做财务系统的开发功能,要求在导出的word文档里面的数字,要以千分位的格式处理显示,于是写了一下下面的方法,希望可以帮助到需要的小伙伴

    /**
    * 格式化数字为千分位显示;
    * @param
    * @return
    */
    public static String fmtMicrometer(String text)

    {

       DecimalFormat df = null;

       if(text.indexOf(".") > 0)

       {

           if(text.length() - text.indexOf(".")-1 == 0)

           {

               df = new DecimalFormat("###,##0.");

           }else if(text.length() - text.indexOf(".")-1 == 1)

           {

               df = new DecimalFormat("###,##0.0");

           }else if(text.length() - text.indexOf(".")-1 == 2)

           {

               df = new DecimalFormat("###,##0.00");

           }else if(text.length() - text.indexOf(".")-1 == 3)

           {

               df = new DecimalFormat("###,##0.000");

           }else if(text.length() - text.indexOf(".")-1 == 4)

           {

               df = new DecimalFormat("###,##0.0000");

           }else if(text.length() - text.indexOf(".")-1 == 5)

           {

               df = new DecimalFormat("###,##0.00000");

           }

       }else
       {

           df = new DecimalFormat("###,##0");

       }

       double number = 0.0;

       try {

           number = Double.parseDouble(text);

       } catch (Exception e) {

           number = 0.0;

       }

       return df.format(number);

    }

    上面的代码主要进行判断小数点的位置,以及小数点前的位置进行格式化的处理,具体的方法:DecimalFormat

    /**
    * Creates a DecimalFormat using the given pattern and the symbols
    * for the default {@link java.util.Locale.Category#FORMAT FORMAT} locale.
    * This is a convenient way to obtain a
    * DecimalFormat when internationalization is not the main concern.
    *


    * To obtain standard formats for a given locale, use the factory methods
    * on NumberFormat such as getNumberInstance. These factories will
    * return the most appropriate sub-class of NumberFormat for a given
    * locale.
    *
    * @param pattern a non-localized pattern string.
    * @exception NullPointerException if pattern is null
    * @exception IllegalArgumentException if the given pattern is invalid.
    * @see java.text.NumberFormat#getInstance
    * @see java.text.NumberFormat#getNumberInstance
    * @see java.text.NumberFormat#getCurrencyInstance
    * @see java.text.NumberFormat#getPercentInstance
    */
    public DecimalFormat(String pattern) {

       // Always applyPattern after the symbols are set
       this.symbols = DecimalFormatSymbols.getInstance(Locale.getDefault(Locale.Category.FORMAT));

       applyPattern(pattern, false);

    }

    占位符的处理;

  • 相关阅读:
    HarmonyOS开发之一——环境安装和HelloWorld
    Autosys Introduction and Architecture
    ChatGPT的问世给哪些行业带来了冲击?
    [附源码]计算机毕业设计springboot时间管理软件app
    Linux基础命令
    消息队列-RabbitMQ(二)
    PL/SQL基本程序结构和语句(三)
    超级实用的程序员接单平台,看完少走几年弯路,强推第一个!
    Ubuntu 18.04下普通用户的一次提权过程
    买卖股票的最佳时机(系列)
  • 原文地址:https://blog.csdn.net/qq_25580555/article/details/127965046