|
| |||||||||
| Tags: class, date, java, number, object, program, string |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| How to format a number in java?
Hi friends, I am new to this forum. I am last year B.Sc.I.T. I am working on project where I use java as front end. I want to display number in particular format. Can anyone tell me How to format a number in java? Please help me to find answer. Thank you. |
|
#2
| ||||
| ||||
| Re: How to format a number in java?
You can format a number in java in following ways. If you want to display some numbers that is formatted to a certain pattern in your project then you can utilize NumberFormat and DecimalFormat class to give you the format that you want. Here I had written example that will show you how to do it. Code:
package org.kodejava.example.text;
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class DecimalFormat
{
public static void main(String[] args)
{
NumberFormat F1 = new DecimalFormat("#0.00");
System.out.println(M);
System.out.println(F1.format(M));
}
} |
|
#3
| |||
| |||
| Re: How to format a number in java?
You have to use pattern of special characters to specify the format of the number. Following example demonstrates some of the characters. Code: // The 0 symbol shows a digit or 0 if no digit present
NumberFormat F1 = new DecimalFormat("000000");
String S = F1.format(-4321.567); // -004321 Code: // The # symbol shows a digit or nothing if no digit present
F1 = new DecimalFormat("##");
S = F1.format(-4321.567); // -4321
S = F1.format(0); // 0
F1 = new DecimalFormat("##00");
S = F1.format(0); // 00 |
|
#4
| ||||
| ||||
| Re: How to format a number in java?
You can format number in following ways. Code: import java.text.NumberFormat;
import java.util.Locale;
public class NFD {
static public void displayNumber(Locale currentLocale) {
Integer q = new Integer(65231);
Double a = new Double(58741.246);
NumberFormat nFormatter;
String qOut;
String aOut;
nFormatter = NumberFormat.getNumberInstance(cLocale);
qOut = numberFormatter.format(q);
aOut = numberFormatter.format(amount);
System.out.println(qOut + " " + cLocale.toString());
System.out.println(aOut + " " + cLocale.toString());
}
static public void displayCurrency(Locale currentLocale) {
Double c = new Double(3652159.21);
NumberFormat cFormatter;
String cOut;
cFormatter = NumberFormat.getCurrencyInstance(cLocale);
cOut = currencyFormatter.format(c);
System.out.println(currencyOut + " " + cLocale.toString());
}
static public void displayPercent(Locale cLocale) {
Double p = new Double(0.75);
NumberFormat pFormatter;
String pOut;
pFormatter = NumberFormat.getPInstance(currentLocale);
pOut = pFormatter.format(p);
System.out.println(pOut + " " + cLocale.toString());
}
static public void main(String[] args) {
Locale[] locales = { new Locale("fr", "FR"), new Locale("de", "DE"),
new Locale("en", "US") };
for (int h = 0; h < locales.length; h++) {
System.out.println();
displayNumber(locales[h]);
displayCurrency(locales[h]);
displayPercent(locales[h]);
}
}
} |
|
#5
| ||||
| ||||
| Re: How to format a number in java?
Hey there are different way to format number. I had written 3 example to do this. Just go through it. Code: double p = 2.6854; System.out.println(p); // p is 2.6854 int q = 3; BigDecimal d = new BigDecimal(p); // setScale is immutable d = d.setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP); p = d.doubleValue(); System.out.println(p); // p is 2.68 Code: d = (float) (Math.round(n*1200.0f)/200.0f); Code: DecimalFormat cms = new DecimalFormat( "#,###,###,##0.00" ); double fg = 100.2397; double fg2erd = new Double(dfg.format(dfg)).doubleValue(); // The value of fg2erddec will be 200.23 |
![]() |
|
| Thread Tools | Search this Thread |
| |
Similar Threads for: "How to format a number in java?" | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Determine a whole number in java | Remedy | Software Development | 5 | 25-02-2010 04:39 AM |
| Random number using java.util | New ID | Software Development | 5 | 10-02-2010 02:22 AM |
| Random number generation in java | GlassFish | Software Development | 3 | 16-11-2009 11:42 AM |
| How to convert vb .text to number format | Brunoz | Software Development | 6 | 04-07-2009 10:18 PM |
| Data exported to excel 2007 changes cell number format to 'general' ? | Vilius Mockûnas | MS Office Support | 1 | 28-06-2009 12:12 PM |