简体   繁体   English

如何制作数字格式?

[英]How to make a number format?

I want to format number which I receive from the local database 我想格式化从本地数据库收到的号码

Example: 例:

10000 to 10.000 or 1000000 to 100.000 10000到10.000或1000000到100.000

My code: 我的代码:

holder.mTextViewHarga.setText("Rp. " + mProdukList.get(position).gethargaProduk());

i have to try this code before but still error: 我必须先尝试此代码但仍然出错:

Locale localeID = new Locale("in", "ID");
NumberFormat formatRupiah = NumberFormat.getCurrencyInstance(localeID);

holder.mTextViewHarga.setText("Rp. " +formatRupiah.format((double) mProdukList.get(position).gethargaProduk()));

You need to use a number formatter as shown in the example below: 您需要使用数字格式化程序,如下例所示:

NumberFormat myFormatter = new DecimalFormat("#.###");
double myNumber = 1234.5632;
String formattedNumber = myFormatter.format(myNumber);

and formattedNumber will be equal to "1234.563" 和formattedNumber将等于“1234.563”

Use DecimalFormat() 使用DecimalFormat()

DecimalFormat df2 = new DecimalFormat( "#,###,###,##0.00" );
double d = 100.2397;
double d1 = new Double(df2.format(d)).doubleValue();

Output - d1 will be 100.24 输出 - d1将为100.24

As from this link , the answer would be 这个链接 ,答案是

Locale localeID = new Locale("in", "ID");
String pattern = "###,###.###";
NumberFormat nf = NumberFormat.getNumberInstance(localeID);
DecimalFormat df = (DecimalFormat)nf;
df.applyPattern(pattern);
String output = df.format(value);

The DecimalFormat class: It is a Java class that allows us to display the numbers with a desired format, it can be a limit the decimals, use the period, comma, etc. example DecimalFormat类:它是一个Java类,允许我们显示具有所需格式的数字,它可以是小数限制,使用句点,逗号等示例

//required import
import java.text.DecimalFormat;
DecimalFormat formatter = new DecimalFormat("####.####");
//It prints four decimals, like this: 7,1234
System.out.println (formatter.format (7.12342383));

If we use zeros instead of #, the non-existent digits will be filled with zeros, an example: 如果我们使用零而不是#,那么不存在的数字将用零填充,例如:

DecimalFormat formatter = new DecimalFormat("0000.0000");
//It prints: 0001,8200
System.out.println (formatter.format (1.82));

We can also use the percent sign (%) in the mask and so the number will be automatically multiplied by 100 at the time of printing. 我们还可以使用掩码中的百分号(%),因此在打印时数字将自动乘以100。

DecimalFormat formatter = new DecimalFormat("###.##%");
//It prints: 68,44%
System.out.println (formatter.format(0.6844));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM