• 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);

    }

    占位符的处理;

  • 相关阅读:
    【蓝牙协议】简介:蓝牙芯片、蓝牙协议架构
    动手学深度学习PyTorch(六):卷积神经网络
    算法通关村第六村-白银挑战树的层序遍历
    C/S - Exploits 学习笔记
    数据结构与算法之图: Leetcode 417. 太平洋大西洋水流问题 (Typescript版)
    数据结构(3)基础查找算法——顺序查找、二分查找(JAVA版)
    小明回家 题解 BFS
    CaiT:Facebook提出高性能深度ViT结构 | ICCV 2021
    嵌入式硬件中常见的100种硬件选型方式
    自媒体写作,怎么写出让人看了就想点击的标题呢?
  • 原文地址:https://blog.csdn.net/qq_25580555/article/details/127965046