Java 数字格式:DecimalFormat

一则或许对你有用的小广告

欢迎加入小哈的星球 ,你将获得:专属的项目实战 / 1v1 提问 / Java 学习路线 / 学习打卡 / 每月赠书 / 社群讨论

  • 新项目:《从零手撸:仿小红书(微服务架构)》 正在持续爆肝中,基于 Spring Cloud Alibaba + Spring Boot 3.x + JDK 17...点击查看项目介绍 ;
  • 《从零手撸:前后端分离博客项目(全栈开发)》 2 期已完结,演示链接: http://116.62.199.48/ ;

截止目前, 星球 内专栏累计输出 63w+ 字,讲解图 2808+ 张,还在持续爆肝中.. 后续还会上新更多项目,目标是将 Java 领域典型的项目都整一波,如秒杀系统, 在线商城, IM 即时通讯,权限管理,Spring Cloud Alibaba 微服务等等,已有 2200+ 小伙伴加入学习 ,欢迎点击围观

Java Numeric Formatting 一文中,我描述并演示了 NumberFormat 静态方法提供的一些有用实例,例如 NumberFormat.getNumberInstance(Locale) NumberFormat.getPercentInstance(Locale) NumberFormat.getCurrencyInstance(Locale) NumberFormat.getIntegerInstance(Locale) ) 。事实证明,抽象 NumberFormat 的所有这些实例实际上都是 DecimalFormat 的实例,它扩展了 NumberFormat

下一个代码清单和相关输出表明 NumberFormat 的“getInstance”方法返回的所有实例实际上都是 DecimalFormat 实例。同一 DecimalFormat 类的这些实例的区别在于它们的属性设置,例如最小和最大整数位数(小数点左侧的数字)以及最小和最大小数位数(小数点右侧的数字) .它们都共享相同的舍入模式和货币设置。

NumberFormat.getInstance() 提供的实例是 DecimalFormat 实例


 /**
 * Write characteristics of provided Currency object to
 * standard output.
 *
 * @param currency Instance of Currency whose attributes
 *    are to be written to standard output.
 */
public void printCurrencyCharacteristics(final Currency currency)
{
   out.print("\tCurrency: " + currency.getCurrencyCode() 
      + "(ISO 4217 Code: " + currency.getNumericCode() + "), ");
   out.println(currency.getSymbol() + ", (" + currency.getDisplayName() + ")");
}

/**

  • Writes characteristics of provided NumberFormat instance
  • to standard output under a heading that includes the provided
  • description.
  • @param numberFormat Instance of NumberFormat whose key
  • characteristics are to be written to standard output.
  • @param description Description to be included in standard
  • output. */ public void printNumberFormatCharacteristics( final NumberFormat numberFormat, final String description) { out.println(description + ": " + numberFormat.getClass().getCanonicalName()); out.println("\tRounding Mode: " + numberFormat.getRoundingMode()); out.println("\tMinimum Fraction Digits: " + numberFormat.getMinimumFractionDigits()); out.println("\tMaximum Fraction Digits: " + numberFormat.getMaximumFractionDigits()); out.println("\tMinimum Integer Digits: " + numberFormat.getMinimumIntegerDigits()); out.println("\tMaximum Integer Digits: " + numberFormat.getMaximumIntegerDigits()); printCurrencyCharacteristics(numberFormat.getCurrency()); if (numberFormat instanceof DecimalFormat) { final DecimalFormat decimalFormat = (DecimalFormat) numberFormat; out.println("\tPattern: " + decimalFormat.toPattern()); } }

/**

  • Display key characteristics of the "standard"
  • NumberFormat/DecimalFormat instances returned by the static
  • NumberFormat methods getIntegerInstance(), getCurrencyInstance(),
  • getPercentInstance(), and getNumberInstance(). */ public void demonstrateDecimalFormatInstancesFromStaticNumberFormatMethods() { final NumberFormat integerInstance = NumberFormat.getIntegerInstance(); printNumberFormatCharacteristics(integerInstance, "IntegerInstance"); final NumberFormat currencyInstance = NumberFormat.getCurrencyInstance(); printNumberFormatCharacteristics(currencyInstance, "CurrencyInstance"); final NumberFormat percentInstance = NumberFormat.getPercentInstance(); printNumberFormatCharacteristics(percentInstance, "PercentInstance"); final NumberFormat numberInstance = NumberFormat.getNumberInstance(); printNumberFormatCharacteristics(numberInstance, "NumberInstance"); }

尽管我之前的帖子和到目前为止的这篇帖子已经演示了通过静态 NumberFormat 访问方法获取 DecimalFormat 的实例,但 DecimalFormat 还具有三个重载构造函数 DecimalFormat() DecimalFormat(String) DecimalFormat(String, DecimalFormatSymbols) 。但是请注意,在 DecimalFormat 的 Javadoc 文档 中有一条警告指出,“一般来说,不要直接调用 DecimalFormat 构造函数,因为 NumberFormat 工厂方法可能会返回 DecimalFormat 以外的子类。”尽管有 Javadoc 警告,但我接下来的几个示例确实使用它们的直接构造函数实例化了 DecimalFormat 实例,因为在这种情况下这样做没有坏处。

DecimalFormat 的实例支持对十进制数字的表示格式进行很大程度的控制。以下代码针对各种不同的自定义模式运行前面示例中使用的标准数字集。代码清单后的屏幕截图显示了应用这些模式时这些数字是如何呈现的。



 /**
 * Write characteristics of provided Currency object to
 * standard output.
 *
 * @param currency Instance of Currency whose attributes
 *    are to be written to standard output.
 */
public void printCurrencyCharacteristics(final Currency currency)
{
   out.print("\tCurrency: " + currency.getCurrencyCode() 
      + "(ISO 4217 Code: " + currency.getNumericCode() + "), ");
   out.println(currency.getSymbol() + ", (" + currency.getDisplayName() + ")");
}

/**

  • Writes characteristics of provided NumberFormat instance
  • to standard output under a heading that includes the provided
  • description.
  • @param numberFormat Instance of NumberFormat whose key
  • characteristics are to be written to standard output.
  • @param description Description to be included in standard
  • output. */ public void printNumberFormatCharacteristics( final NumberFormat numberFormat, final String description) { out.println(description + ": " + numberFormat.getClass().getCanonicalName()); out.println("\tRounding Mode: " + numberFormat.getRoundingMode()); out.println("\tMinimum Fraction Digits: " + numberFormat.getMinimumFractionDigits()); out.println("\tMaximum Fraction Digits: " + numberFormat.getMaximumFractionDigits()); out.println("\tMinimum Integer Digits: " + numberFormat.getMinimumIntegerDigits()); out.println("\tMaximum Integer Digits: " + numberFormat.getMaximumIntegerDigits()); printCurrencyCharacteristics(numberFormat.getCurrency()); if (numberFormat instanceof DecimalFormat) { final DecimalFormat decimalFormat = (DecimalFormat) numberFormat; out.println("\tPattern: " + decimalFormat.toPattern()); } }

/**

  • Display key characteristics of the "standard"
  • NumberFormat/DecimalFormat instances returned by the static
  • NumberFormat methods getIntegerInstance(), getCurrencyInstance(),
  • getPercentInstance(), and getNumberInstance(). */ public void demonstrateDecimalFormatInstancesFromStaticNumberFormatMethods() { final NumberFormat integerInstance = NumberFormat.getIntegerInstance(); printNumberFormatCharacteristics(integerInstance, "IntegerInstance"); final NumberFormat currencyInstance = NumberFormat.getCurrencyInstance(); printNumberFormatCharacteristics(currencyInstance, "CurrencyInstance"); final NumberFormat percentInstance = NumberFormat.getPercentInstance(); printNumberFormatCharacteristics(percentInstance, "PercentInstance"); final NumberFormat numberInstance = NumberFormat.getNumberInstance(); printNumberFormatCharacteristics(numberInstance, "NumberInstance"); }

 /**
 * Write characteristics of provided Currency object to
 * standard output.
 *
 * @param currency Instance of Currency whose attributes
 *    are to be written to standard output.
 */
public void printCurrencyCharacteristics(final Currency currency)
{
   out.print("\tCurrency: " + currency.getCurrencyCode() 
      + "(ISO 4217 Code: " + currency.getNumericCode() + "), ");
   out.println(currency.getSymbol() + ", (" + currency.getDisplayName() + ")");
}

/**
 * Writes characteristics of provided NumberFormat instance
 * to standard output under a heading that includes the provided
 * description.
 *
 * @param numberFormat Instance of NumberFormat whose key
 *    characteristics are to be written to standard output.
 * @param description Description to be included in standard
 *    output.
 */
public void printNumberFormatCharacteristics(
   final NumberFormat numberFormat, final String description)
{
   out.println(description + ": " + numberFormat.getClass().getCanonicalName());
   out.println("\tRounding Mode:           " + numberFormat.getRoundingMode());
   out.println("\tMinimum Fraction Digits: " + numberFormat.getMinimumFractionDigits());
   out.println("\tMaximum Fraction Digits: " + numberFormat.getMaximumFractionDigits());
   out.println("\tMinimum Integer Digits:  " + numberFormat.getMinimumIntegerDigits());
   out.println("\tMaximum Integer Digits:  " + numberFormat.getMaximumIntegerDigits());
   printCurrencyCharacteristics(numberFormat.getCurrency());
   if (numberFormat instanceof DecimalFormat)
   {
      final DecimalFormat decimalFormat = (DecimalFormat) numberFormat;
      out.println("\tPattern: " + decimalFormat.toPattern());
   }
}

/**
 * Display key characteristics of the "standard"
 * NumberFormat/DecimalFormat instances returned by the static
 * NumberFormat methods getIntegerInstance(), getCurrencyInstance(),
 * getPercentInstance(), and getNumberInstance().
 */
public void demonstrateDecimalFormatInstancesFromStaticNumberFormatMethods()
{
   final NumberFormat integerInstance = NumberFormat.getIntegerInstance();
   printNumberFormatCharacteristics(integerInstance, "IntegerInstance");
   final NumberFormat currencyInstance = NumberFormat.getCurrencyInstance();
   printNumberFormatCharacteristics(currencyInstance, "CurrencyInstance");
   final NumberFormat percentInstance = NumberFormat.getPercentInstance();
   printNumberFormatCharacteristics(percentInstance, "PercentInstance");
   final NumberFormat numberInstance = NumberFormat.getNumberInstance();
   printNumberFormatCharacteristics(numberInstance, "NumberInstance");
}

对于我最后两个应用 DecimalFormat 的示例,我将通过使用 NumberFormat.getInstance(Locale) 的首选方法获取 DecimalFormat 的实例。第一个代码清单演示了应用于同一个双精度的不同语言环境,然后是每个语言环境的输出格式。


 /**
 * Write characteristics of provided Currency object to
 * standard output.
 *
 * @param currency Instance of Currency whose attributes
 *    are to be written to standard output.
 */
public void printCurrencyCharacteristics(final Currency currency)
{
   out.print("\tCurrency: " + currency.getCurrencyCode() 
      + "(ISO 4217 Code: " + currency.getNumericCode() + "), ");
   out.println(currency.getSymbol() + ", (" + currency.getDisplayName() + ")");
}

/**

  • Writes characteristics of provided NumberFormat instance
  • to standard output under a heading that includes the provided
  • description.
  • @param numberFormat Instance of NumberFormat whose key
  • characteristics are to be written to standard output.
  • @param description Description to be included in standard
  • output. */ public void printNumberFormatCharacteristics( final NumberFormat numberFormat, final String description) { out.println(description + ": " + numberFormat.getClass().getCanonicalName()); out.println("\tRounding Mode: " + numberFormat.getRoundingMode()); out.println("\tMinimum Fraction Digits: " + numberFormat.getMinimumFractionDigits()); out.println("\tMaximum Fraction Digits: " + numberFormat.getMaximumFractionDigits()); out.println("\tMinimum Integer Digits: " + numberFormat.getMinimumIntegerDigits()); out.println("\tMaximum Integer Digits: " + numberFormat.getMaximumIntegerDigits()); printCurrencyCharacteristics(numberFormat.getCurrency()); if (numberFormat instanceof DecimalFormat) { final DecimalFormat decimalFormat = (DecimalFormat) numberFormat; out.println("\tPattern: " + decimalFormat.toPattern()); } }

/**

  • Display key characteristics of the "standard"
  • NumberFormat/DecimalFormat instances returned by the static
  • NumberFormat methods getIntegerInstance(), getCurrencyInstance(),
  • getPercentInstance(), and getNumberInstance(). */ public void demonstrateDecimalFormatInstancesFromStaticNumberFormatMethods() { final NumberFormat integerInstance = NumberFormat.getIntegerInstance(); printNumberFormatCharacteristics(integerInstance, "IntegerInstance"); final NumberFormat currencyInstance = NumberFormat.getCurrencyInstance(); printNumberFormatCharacteristics(currencyInstance, "CurrencyInstance"); final NumberFormat percentInstance = NumberFormat.getPercentInstance(); printNumberFormatCharacteristics(percentInstance, "PercentInstance"); final NumberFormat numberInstance = NumberFormat.getNumberInstance(); printNumberFormatCharacteristics(numberInstance, "NumberInstance"); }

 /**
 * Write characteristics of provided Currency object to
 * standard output.
 *
 * @param currency Instance of Currency whose attributes
 *    are to be written to standard output.
 */
public void printCurrencyCharacteristics(final Currency currency)
{
   out.print("\tCurrency: " + currency.getCurrencyCode() 
      + "(ISO 4217 Code: " + currency.getNumericCode() + "), ");
   out.println(currency.getSymbol() + ", (" + currency.getDisplayName() + ")");
}

/**
 * Writes characteristics of provided NumberFormat instance
 * to standard output under a heading that includes the provided
 * description.
 *
 * @param numberFormat Instance of NumberFormat whose key
 *    characteristics are to be written to standard output.
 * @param description Description to be included in standard
 *    output.
 */
public void printNumberFormatCharacteristics(
   final NumberFormat numberFormat, final String description)
{
   out.println(description + ": " + numberFormat.getClass().getCanonicalName());
   out.println("\tRounding Mode:           " + numberFormat.getRoundingMode());
   out.println("\tMinimum Fraction Digits: " + numberFormat.getMinimumFractionDigits());
   out.println("\tMaximum Fraction Digits: " + numberFormat.getMaximumFractionDigits());
   out.println("\tMinimum Integer Digits:  " + numberFormat.getMinimumIntegerDigits());
   out.println("\tMaximum Integer Digits:  " + numberFormat.getMaximumIntegerDigits());
   printCurrencyCharacteristics(numberFormat.getCurrency());
   if (numberFormat instanceof DecimalFormat)
   {
      final DecimalFormat decimalFormat = (DecimalFormat) numberFormat;
      out.println("\tPattern: " + decimalFormat.toPattern());
   }
}

/**
 * Display key characteristics of the "standard"
 * NumberFormat/DecimalFormat instances returned by the static
 * NumberFormat methods getIntegerInstance(), getCurrencyInstance(),
 * getPercentInstance(), and getNumberInstance().
 */
public void demonstrateDecimalFormatInstancesFromStaticNumberFormatMethods()
{
   final NumberFormat integerInstance = NumberFormat.getIntegerInstance();
   printNumberFormatCharacteristics(integerInstance, "IntegerInstance");
   final NumberFormat currencyInstance = NumberFormat.getCurrencyInstance();
   printNumberFormatCharacteristics(currencyInstance, "CurrencyInstance");
   final NumberFormat percentInstance = NumberFormat.getPercentInstance();
   printNumberFormatCharacteristics(percentInstance, "PercentInstance");
   final NumberFormat numberInstance = NumberFormat.getNumberInstance();
   printNumberFormatCharacteristics(numberInstance, "NumberInstance");
}

到目前为止,我的 DecimalFormat 示例侧重于格式化数字以供演示。最后一个示例从另一个方向开始,从字符串表示中解析一个值。


 /**
 * Write characteristics of provided Currency object to
 * standard output.
 *
 * @param currency Instance of Currency whose attributes
 *    are to be written to standard output.
 */
public void printCurrencyCharacteristics(final Currency currency)
{
   out.print("\tCurrency: " + currency.getCurrencyCode() 
      + "(ISO 4217 Code: " + currency.getNumericCode() + "), ");
   out.println(currency.getSymbol() + ", (" + currency.getDisplayName() + ")");
}

/**

  • Writes characteristics of provided NumberFormat instance
  • to standard output under a heading that includes the provided
  • description.
  • @param numberFormat Instance of NumberFormat whose key
  • characteristics are to be written to standard output.
  • @param description Description to be included in standard
  • output. */ public void printNumberFormatCharacteristics( final NumberFormat numberFormat, final String description) { out.println(description + ": " + numberFormat.getClass().getCanonicalName()); out.println("\tRounding Mode: " + numberFormat.getRoundingMode()); out.println("\tMinimum Fraction Digits: " + numberFormat.getMinimumFractionDigits()); out.println("\tMaximum Fraction Digits: " + numberFormat.getMaximumFractionDigits()); out.println("\tMinimum Integer Digits: " + numberFormat.getMinimumIntegerDigits()); out.println("\tMaximum Integer Digits: " + numberFormat.getMaximumIntegerDigits()); printCurrencyCharacteristics(numberFormat.getCurrency()); if (numberFormat instanceof DecimalFormat) { final DecimalFormat decimalFormat = (DecimalFormat) numberFormat; out.println("\tPattern: " + decimalFormat.toPattern()); } }

/**

  • Display key characteristics of the "standard"
  • NumberFormat/DecimalFormat instances returned by the static
  • NumberFormat methods getIntegerInstance(), getCurrencyInstance(),
  • getPercentInstance(), and getNumberInstance(). */ public void demonstrateDecimalFormatInstancesFromStaticNumberFormatMethods() { final NumberFormat integerInstance = NumberFormat.getIntegerInstance(); printNumberFormatCharacteristics(integerInstance, "IntegerInstance"); final NumberFormat currencyInstance = NumberFormat.getCurrencyInstance(); printNumberFormatCharacteristics(currencyInstance, "CurrencyInstance"); final NumberFormat percentInstance = NumberFormat.getPercentInstance(); printNumberFormatCharacteristics(percentInstance, "PercentInstance"); final NumberFormat numberInstance = NumberFormat.getNumberInstance(); printNumberFormatCharacteristics(numberInstance, "NumberInstance"); }




 /**
 * Write characteristics of provided Currency object to
 * standard output.
 *
 * @param currency Instance of Currency whose attributes
 *    are to be written to standard output.
 */
public void printCurrencyCharacteristics(final Currency currency)
{
   out.print("\tCurrency: " + currency.getCurrencyCode() 
      + "(ISO 4217 Code: " + currency.getNumericCode() + "), ");
   out.println(currency.getSymbol() + ", (" + currency.getDisplayName() + ")");
}

/**

  • Writes characteristics of provided NumberFormat instance
  • to standard output under a heading that includes the provided
  • description.
  • @param numberFormat Instance of NumberFormat whose key
  • characteristics are to be written to standard output.
  • @param description Description to be included in standard
  • output. */ public void printNumberFormatCharacteristics( final NumberFormat numberFormat, final String description) { out.println(description + ": " + numberFormat.getClass().getCanonicalName()); out.println("\tRounding Mode: " + numberFormat.getRoundingMode()); out.println("\tMinimum Fraction Digits: " + numberFormat.getMinimumFractionDigits()); out.println("\tMaximum Fraction Digits: " + numberFormat.getMaximumFractionDigits()); out.println("\tMinimum Integer Digits: " + numberFormat.getMinimumIntegerDigits()); out.println("\tMaximum Integer Digits: " + numberFormat.getMaximumIntegerDigits()); printCurrencyCharacteristics(numberFormat.getCurrency()); if (numberFormat instanceof DecimalFormat) { final DecimalFormat decimalFormat = (DecimalFormat) numberFormat; out.println("\tPattern: " + decimalFormat.toPattern()); } }

/**

  • Display key characteristics of the "standard"
  • NumberFormat/DecimalFormat instances returned by the static
  • NumberFormat methods getIntegerInstance(), getCurrencyInstance(),
  • getPercentInstance(), and getNumberInstance(). */ public void demonstrateDecimalFormatInstancesFromStaticNumberFormatMethods() { final NumberFormat integerInstance = NumberFormat.getIntegerInstance(); printNumberFormatCharacteristics(integerInstance, "IntegerInstance"); final NumberFormat currencyInstance = NumberFormat.getCurrencyInstance(); printNumberFormatCharacteristics(currencyInstance, "CurrencyInstance"); final NumberFormat percentInstance = NumberFormat.getPercentInstance(); printNumberFormatCharacteristics(percentInstance, "PercentInstance"); final NumberFormat numberInstance = NumberFormat.getNumberInstance(); printNumberFormatCharacteristics(numberInstance, "NumberInstance"); }


显示的最后一个示例实际上不需要访问具体的 DecimalNumber 方法,并且能够单独使用 NumberFormat 广告方法。该示例使用 NumberFormat.format(double) 格式化货币表示,然后使用 NumberFormat.parse(String) 解析提供的货币表示以返回原始值。


NumberFormat ,更具体地说 DoubleFormat ,“为任何语言环境格式化和解析数字。”





相关文章