java中怎么把系统格式化_如何在Java中格式化数字?

如何在Java中格式化数字?

如何在Java中格式化数字?

什么是“最佳实践”?

在格式化之前,我需要对数字进行舍入吗?

.7323 =>0.73

.7323 =>0.73

等等

9个解决方案

116 votes

从这个线程,有不同的方法来做到这一点:

double r = 5.1234;

System.out.println(r); // r is 5.1234

int decimalPlaces = 2;

BigDecimal bd = new BigDecimal(r);

// setScale is immutable

bd = bd.setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP);

r = bd.doubleValue();

System.out.println(r); // r is 5.12

f = (float) (Math.round(n*100.0f)/100.0f);

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

double dd = 100.2397;

double dd2dec = new Double(df2.format(dd)).doubleValue();

// The value of dd2dec will be 100.24

DecimalFormat()似乎是最有活力的方式,在阅读其他代码时也很容易理解。

Espo answered 2019-07-21T11:11:03Z

72 votes

您和String.format()将是新的最好的朋友!

[http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html#syntax]

String.format("%.2f", (double)value);

Jeff Atwood answered 2019-07-21T11:11:35Z

14 votes

请注意,从NumberFormat(以及大多数其他Format后代)下降的类不会同步。 创建格式对象并将它们存储在util类中的静态变量中是一种常见(但危险)的做法。 在实践中,它几乎总是有效,直到它开始经历重大负载。

Robert J. Walker answered 2019-07-21T11:12:01Z

11 votes

回合数,是的。 这是主要的示例源。

/*

* Copyright (c) 1995 - 2008 Sun Microsystems, Inc. All rights reserved.

*

* Redistribution and use in source and binary forms, with or without

* modification, are permitted provided that the following conditions

* are met:

*

* - Redistributions of source code must retain the above copyright

* notice, this list of conditions and the following disclaimer.

*

* - Redistributions in binary form must reproduce the above copyright

* notice, this list of conditions and the following disclaimer in the

* documentation and/or other materials provided with the distribution.

*

* - Neither the name of Sun Microsystems nor the names of its

* contributors may be used to endorse or promote products derived

* from this software without specific prior written permission.

*

* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS

* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,

* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR

* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR

* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,

* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,

* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR

* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF

* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING

* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS

* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

import java.util.*;

import java.text.*;

public class DecimalFormatDemo {

static public void customFormat(String pattern, double value ) {

DecimalFormat myFormatter = new DecimalFormat(pattern);

String output = myFormatter.format(value);

System.out.println(value + " " + pattern + " " + output);

}

static public void localizedFormat(String pattern, double value, Locale loc ) {

NumberFormat nf = NumberFormat.getNumberInstance(loc);

DecimalFormat df = (DecimalFormat)nf;

df.applyPattern(pattern);

String output = df.format(value);

System.out.println(pattern + " " + output + " " + loc.toString());

}

static public void main(String[] args) {

customFormat("###,###.###", 123456.789);

customFormat("###.##", 123456.789);

customFormat("000000.000", 123.78);

customFormat("$###,###.###", 12345.67);

customFormat("\u00a5###,###.###", 12345.67);

Locale currentLocale = new Locale("en", "US");

DecimalFormatSymbols unusualSymbols = new DecimalFormatSymbols(currentLocale);

unusualSymbols.setDecimalSeparator('|');

unusualSymbols.setGroupingSeparator('^');

String strange = "#,##0.###";

DecimalFormat weirdFormatter = new DecimalFormat(strange, unusualSymbols);

weirdFormatter.setGroupingSize(4);

String bizarre = weirdFormatter.format(12345.678);

System.out.println(bizarre);

Locale[] locales = {

new Locale("en", "US"),

new Locale("de", "DE"),

new Locale("fr", "FR")

};

for (int i = 0; i < locales.length; i++) {

localizedFormat("###,###.###", 123456.789, locales[i]);

}

}

}

Vinko Vrsalovic answered 2019-07-21T11:12:26Z

8 votes

试试这个:

String.format("%.2f", 32.302342342342343);

简单高效。

Nautilus answered 2019-07-21T11:12:54Z

5 votes

使用DecimalFormat。

ckpwong answered 2019-07-21T11:13:13Z

4 votes

标准库中有两种方法。 一种是使用java.text.DecimalFormat。 基于java.util.Formatter的其他更加神秘的方法(String.format,PrintStream.printf等)应该让C程序员高兴(ish)。

Tom Hawtin - tackline answered 2019-07-21T11:13:38Z

2 votes

正如Robert在他的回答中指出的那样:DecimalFormat既不是同步的,也不是API保证线程安全(它可能取决于你正在使用的JVM版本/供应商)。

使用Spring的Numberformatter代替,这是线程安全的。

Stefan Haberl answered 2019-07-21T11:14:10Z

1 votes

public static void formatDouble(double myDouble){

NumberFormat numberFormatter = new DecimalFormat("##.000");

String result = numberFormatter.format(myDouble);

System.out.println(result);

}

例如,如果传递给formatDouble()方法的double值是345.9372,则以下将是结果是:345.937同样,如果将值.7697传递给方法,则结果如下:0.770

snr answered 2019-07-21T11:14:35Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值