tp框架做商城网站怎么用缓存,wordpress u-degin,镇江佳鑫网络科技有限公司,亚马逊跨境电商平台官网java数字格式化当我看到其他人编写不必要的Java代码并且由于缺乏对已经提供所需功能的JDK类的了解而编写了不必要的Java代码时#xff0c;我会想到很多次。 这样的一个例子是时间相关的常量的使用硬编码值的写入#xff0c;如60 #xff0c; 24 #xff0c; 1440 #xff… java数字格式化 当我看到其他人编写不必要的Java代码并且由于缺乏对已经提供所需功能的JDK类的了解而编写了不必要的Java代码时我会想到很多次。 这样的一个例子是时间相关的常量的使用硬编码值的写入如60 24 1440 和86400时TIMEUNIT提供了更好的标准化的方法。 在这篇文章中我看一看一个类的示例该示例提供了开发人员经常在其上实现的功能 NumberFormat 。 NumberFormat类是java.text包的一部分该包还包括常用的DateFormat和SimpleDateFormat类。 NumberFormat是一个抽象类没有公共构造函数其后代的实例是通过具有诸如getInstance getCurrencyInstanceInstance和getPercentInstance之类的名称的重载静态方法获得的。 货币 下一个代码清单演示了如何调用NumberFormat.getCurrencyInstanceLocale来获取NumberFormat的实例该实例以货币友好格式显示数字。 演示NumberFormat的货币支持 /*** Demonstrate use of a Currency Instance of NumberFormat.*/
public void demonstrateCurrency()
{writeHeaderToStandardOutput(Currency NumberFormat Examples);final NumberFormat currencyFormat NumberFormat.getCurrencyInstance(Locale.US);out.println(15.5 - currencyFormat.format(15.5));out.println(15.54 - currencyFormat.format(15.54));out.println(15.345 - currencyFormat.format(15.345)); // rounds to two decimal placesprintCurrencyDetails(currencyFormat.getCurrency());
}/*** Print out details of provided instance of Currency.** param currency Instance of Currency from which details* will be written to standard output.*/
public void printCurrencyDetails(final Currency currency)
{out.println(Concurrency: currency);out.println(\tISO 4217 Currency Code: currency.getCurrencyCode());out.println(\tISO 4217 Numeric Code: currency.getNumericCode());out.println(\tCurrency Display Name: currency.getDisplayName(Locale.US));out.println(\tCurrency Symbol: currency.getSymbol(Locale.US));out.println(\tCurrency Default Fraction Digits: currency.getDefaultFractionDigits());
} 执行上述代码后结果如下所示 Currency NumberFormat Examples15.5 - $15.50
15.54 - $15.54
15.345 - $15.35
Concurrency: USDISO 4217 Currency Code: USDISO 4217 Numeric Code: 840Currency Display Name: US DollarCurrency Symbol: $Currency Default Fraction Digits: 2 上面的代码和相关的输出表明用于货币的NumberFormat实例实际上是DecimalFormat 会根据语言环境自动应用适当的位数和适当的货币符号。 百分比 下一个代码清单和相关的输出演示了NumberFormat使用以百分比友好格式显示数字。 演示NumberFormat的百分比格式 /*** Demonstrate use of a Percent Instance of NumberFormat.*/
public void demonstratePercentage()
{writeHeaderToStandardOutput(Percentage NumberFormat Examples);final NumberFormat percentageFormat NumberFormat.getPercentInstance(Locale.US);out.println(Instance of: percentageFormat.getClass().getCanonicalName());out.println(1 - percentageFormat.format(1));// will be 0 because truncated to Integer by Integer divisionout.println(75/100 - percentageFormat.format(75/100));out.println(.75 - percentageFormat.format(.75));out.println(75.0/100 - percentageFormat.format(75.0/100));// will be 0 because truncated to Integer by Integer divisionout.println(83/93 - percentageFormat.format((83/93)));out.println(93/83 - percentageFormat.format(93/83));out.println(.5 - percentageFormat.format(.5));out.println(.912 - percentageFormat.format(.912));out.println(---- Setting Minimum Fraction Digits to 1:);percentageFormat.setMinimumFractionDigits(1);out.println(1 - percentageFormat.format(1));out.println(.75 - percentageFormat.format(.75));out.println(75.0/100 - percentageFormat.format(75.0/100));out.println(.912 - percentageFormat.format(.912));
}Percentage NumberFormat Examples1 - 100%
75/100 - 0%
.75 - 75%
75.0/100 - 75%
83/93 - 0%
93/83 - 100%
.5 - 50%
.912 - 91%
---- Setting Minimum Fraction Digits to 1:
1 - 100.0%
.75 - 75.0%
75.0/100 - 75.0%
.912 - 91.2% 代码和百分比的输出NumberFormat使用表明通过默认的实例NumberFormat 实际上是一个DecimalFormat通过返回在这种情况下 NumberFormat.getPercentInstance区域的方法没有小数位乘所提供的数目由100假定它是如果提供则为百分数的十进制等效值 并添加一个百分号。 整数 接下来显示的少量代码及其相关输出演示了NumberFormat使用以整数格式显示数字。 演示NumberFormat的整数格式 /*** Demonstrate use of an Integer Instance of NumberFormat.*/
public void demonstrateInteger()
{writeHeaderToStandardOutput(Integer NumberFormat Examples);final NumberFormat integerFormat NumberFormat.getIntegerInstance(Locale.US);out.println(7.65 - integerFormat.format(7.65));out.println(7.5 - integerFormat.format(7.5));out.println(7.49 - integerFormat.format(7.49));out.println(-23.23 - integerFormat.format(-23.23));
}Integer NumberFormat Examples7.65 - 8
7.5 - 8
7.49 - 7
-23.23 - -23 如上面的代码和相关输出所示 NumberFormat方法getIntegerInstanceLocale返回一个实例该实例将提供的数字表示为整数。 固定位数 下一个代码清单和相关的输出演示了如何使用NumberFormat打印浮点数的定点表示形式。 换句话说使用NumberFormat可以表示一个数字该数字在小数点左边“整数”数字和小数点右边“小数”数字正好具有规定位数。 演示定点数字的NumberFormat /*** Demonstrate generic NumberFormat instance with rounding mode,* maximum fraction digits, and minimum integer digits specified.*/
public void demonstrateNumberFormat()
{writeHeaderToStandardOutput(NumberFormat Fixed-Point Examples);final NumberFormat numberFormat NumberFormat.getNumberInstance();numberFormat.setRoundingMode(RoundingMode.HALF_UP);numberFormat.setMaximumFractionDigits(2);numberFormat.setMinimumIntegerDigits(1);out.println(numberFormat.format(234.234567));out.println(numberFormat.format(1));out.println(numberFormat.format(.234567));out.println(numberFormat.format(.349));out.println(numberFormat.format(.3499));out.println(numberFormat.format(0.9999));
}NumberFormat Fixed-Point Examples234.23
1
0.23
0.34
0.35
1 上面的代码和相关的输出演示了对最小“整数”位数的精确控制该位数代表小数点左边至少一个因此在适用时显示为零和最大“分数”小数点右边的数字。 尽管未显示但也可以指定最大整数位数和最小分数位数。 结论 我曾经用过这篇文章来研究如何使用NumberFormat来以不同的方式货币百分比整数固定的小数点等表示数字并且通常意味着不需要编写或减少代码来对这些数字进行按摩格式。 当我第一次开始写这篇文章时我设想包括有关NumberFormat的直接后代 DecimalFormat和ChoiceFormat 的示例和讨论但是已经确定这篇文章已经足够冗长了。 我可能会在以后的博客文章中介绍NumberFormat这些后代。 翻译自: https://www.javacodegeeks.com/2014/08/java-numeric-formatting.htmljava数字格式化