in Java, how do I get the decimal-symbol that is set in the system?
I DON'T want to rely on the Locale , since it could be changed regardless of the locale.
haven't seen something in System.getProperties(), but maybe I'm missing something
Thanks
Here is an example using the DecimalFormatSymbols class. You said you do not want to rely on Locale... Well, you can specify your Locale in the initialization on the object. But you don't have to.
DecimalFormatSymbols sym = new DecimalFormatSymbols(); //Can specify 'Locale' here
char charDecimal = sym.getDecimalSeparator();
String strDecimal = String.valueOf(charDecimal); //Value becomes '.'
Hope that helps! :D
You can use system properties to get default locale and then get the decimal-symbol.
As you don't want to rely on Locale.getDefault().
String language = System.getProperty("user.language");
String country = System.getProperty("user.country");
NumberFormat nf = NumberFormat.getNumberInstance(new Locale(language, country));
DecimalFormat df = (DecimalFormat)nf;
DecimalFormatSymbols symbols=format.getDecimalFormatSymbols();
char sep=symbols.getDecimalSeparator();
Related
I'm trying to find a way to display currency with a dot so for instance it should be
1.234,56 kr.
At the moment I'm using
pattern = "#,##0.00 ¤";
new DecimalFormat(pattern);
This doesn't work as the Danish krone is for some reason defined there as kr instead of officially recognized kr.
I've looked for a way to escape these characters using Unicode value that I would add to pattern but that doesn't work. In the official documentation here I don't see a way to do it either.
TLDR: I want to add full stop after currency symbol. So at the moment I have it like this kr , what I want to get is kr. .
The output of currency values depends on the respective country setting. If you want to explicitly have a decimal point as in your case, you have to set a corresponding locale for the Numberformat. E.g. English for a point.
For Example:
public String FormatWithPoint(double yourValue){
NumberFormat nf = NumberFormat.getNumberInstance(Locale.ENGLISH);
nf.setGroupingUsed(false);
return nf.format(yourValue);
}
Edit: If you need more control, you can also do the following:
public String FormatCurreny(double yourValue){
NumberFormat nf = NumberFormat.getCurrencyInstance();
nf.setGroupingUsed(false);
DecimalFormatSymbols dfs = new DecimalFormatSymbols();
dfs.setCurrencySymbol("kr.");
dfs.setMonetaryDecimalSeparator('.');
((DecimalFormat) nf).setDecimalFormatSymbols(dfs);
return nf.format(yourValue);
}
If you just want the dot after the currency symbol you can set a custom currency symbol. Here I did it using euros (note that instead of "€." you can put my_symbol.getCurrencySymbol()+"." if you want it for all currency and not just one):
DecimalFormatSymbols my_symbol = new DecimalFormatSymbols();
my_symbol.setCurrencySymbol("€.");
String pattern = "###,###.###¤";
DecimalFormat decimalFormat1 = new DecimalFormat(pattern, my_symbol);
String format = decimalFormat1.format(987654321.321);
System.out.println(format);
This gives
987.654.321,321€.
I have tried
System.out.println(myLorry.toString(registration, myCar.calcCharge()));
which outputs
Registration: TA17 NDD Charge: 7.0
I want my program to output
Registration: TA17 NDD Charge: £7.00
How can I format this correctly?
EDIT:
Why doesn't formatting work correctly? It says it's expecting two parameters but can only find one. I need to call objects using the toString method.
System.out.printf("%s £%.2f" ,myCar.toString(registration, myCar.calcCharge()));
like #davidxxx suggest in comment you can use
DecimalFormat d = new DecimalFormat("'£'0.00");
System.out.println(d.format(7.0));
Output
£7,00
If you have a problem with dot(.) and comma(,) then you can use DecimalFormatSymbols :
DecimalFormat d = new DecimalFormat("'£'0.00");
DecimalFormatSymbols sym = DecimalFormatSymbols.getInstance();
sym.setDecimalSeparator('.');
d.setDecimalFormatSymbols(sym);
one solution is to use the printf.
e.g.
double value = 7.0;
System.out.printf("%s%.2f","£", value);
output:
£7.00
In fact you should consider two things :
formatting the number value with the fixed number of digits for the floating part.
setting the decimal separator character.
The second point may matter as according to the locale set by the JVM, you could get a distinct result : £7.00 or £7,00
So, you could specify the "£0.00" pattern in DecimalFormat and create the DecimalFormat instance with a specific DecimalFormatSymbols that ensures that you will use as decimal symbol the . character.
You could do it for example :
float f = 7;
DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols();
otherSymbols.setDecimalSeparator('.');
NumberFormat formatter = new DecimalFormat("£0.00", otherSymbols);
String valueFormated = formatter.format(f);
But in fact a more simple way would be to use the String.format() method by specifying both the expected pattern (two digits for the floating part) and a Locale that uses the . as decimal separator :
float f = 7;
String valueFormated = String.format(Locale.US, "£%.2f", f);
Solved it! I was looking in completely the wrong part of my program, here is my solution:
String toString(String rn, double calcCharge)
{
DecimalFormat d = new DecimalFormat("£0.00");
return "Registration: " + rn + " Charge: " + d.format(calcCharge());
}
I had to modify a class that my subclasses inherited from.
is there a class in Java that lets you format a number like "102203345.32" to this "102.203.345,32" and return a string type?
I would like to obtain a String where the thousands are separated by the '.' and the decimals are separated by a comma ','.
Could someone help me please? I found a class DecimalFormat and I tried to customize it:
public class CustomDecimalFormat {
static public String customFormat(String pattern, double value ) {
DecimalFormat myFormatter = new DecimalFormat(pattern);
String output = myFormatter.format(value);
return output;
}
}
but when I call the customFormat method like this: CustomDecimalFormat.customFormat("###.###,00") I get an exception...
What should I do?
Thanks!
Be sure to read and understand the Special Pattern Characters section of the Javadoc, especially this note:
The characters listed here are used in non-localized patterns. Localized patterns use the corresponding characters taken from this formatter's DecimalFormatSymbols object instead, and these characters lose their special status.
If you have done that, it should be clear to you that you must use the appropriate constructor and supply the appropriately configured separator/grouping chars, whereas in the pattern itself the dot and the comma have a special meaning.
All the complexity above is there for your convenience, actually: it allows you to customize the number format and have it localized.
Here's a code sample which worked for me:
final DecimalFormatSymbols syms = new DecimalFormatSymbols();
syms.setDecimalSeparator(',');
syms.setGroupingSeparator('.');
DecimalFormat myFormatter = new DecimalFormat("###,###.00", syms);
System.out.println(myFormatter.format(1234.12));
You can also use a variant where you apply the localized pattern, for more intuitive code:
final DecimalFormatSymbols syms = new DecimalFormatSymbols();
syms.setDecimalSeparator(',');
syms.setGroupingSeparator('.');
DecimalFormat myFormatter = new DecimalFormat("", syms);
myFormatter.applyLocalizedPattern("###.###,00");
System.out.println(myFormatter.format(1234.12));
First of all,
you misplaced the comma and decimal points. Your format should be : ###,###.00 instead of ###.###,00 ...
Also check with your Locale, it has an effect on the format. See the link below.
http://docs.oracle.com/javase/tutorial/i18n/format/decimalFormat.html
You can try GERMAN number format
NumberFormat.getNumberInstance(Locale.GERMAN).format(102203345.32)
DecimalFormat df2 = new DecimalFormat("#.##");
double zipf = 0.23951367781155017;
String zipt = df2.format(zipf);
System.out.println(zipt);
And I get "0,24"
The problem with this is then I want to use it as a double. But the Double.valueOf(); method fails due to the comma being there in the string output. Any way to solve this?
For decimal dot, you should create an instance with english locale like this:
NumberFormat nf = NumberFormat.getNumberInstance(Locale.ENGLISH);
nf.setMinimumFractionDigits(2);
nf.setMaximumFractionDigits(2);
String zipt = nf.format(zipf);
System.out.println(zipt);
I also suggest setting rounding to HALF_UP, because default rounding is not what most of us would expect: http://docs.oracle.com/javase/1.5.0/docs/api/java/math/BigDecimal.html#ROUND_HALF_EVEN
nf.setRoundingMode(RoundingMode.HALF_UP);
Use different locale.German has dot
NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN);
DecimalFormat df = (DecimalFormat)nf;
Alternative woud be to use string and then modify string to your needs.After that just parse to double.All done :)
Your problems is the local that your JVM is using , try to change at your current local.
Use DecimalFormat constructor that allows you to specify locale
new DecimalFormat("#.##", new DecimalFormatSymbols(new Locale("en")));
you could "format" your double manually but cutting of the decimal places like this:
DecimalFormat df2 = new DecimalFormat("#.##");
double zipf = 0.23951367781155017;
String zipt = df2.format(zipf);
System.out.println(zipt);
long zipfLong = Math.round(zipf*100);
double zipfDouble = zipfLong/100.0;
System.out.println(zipfDouble);
with Math.round you make sure the that 0.239.. becomes 0.24. zipf*100 will "cut" off the additional decimal places and zipfLong/100.0 will add the decimal places again. Sorry, bad explanation but here is the output:
0,24
0.24
And you can reuse the new zipfDouble as a double value without casting or taking care of locale settings.
I would like to format an amount : the required format is : #.##0,00
example : 299.552.698,05 or 299.552.698,00
When I try to use
(new DecimalFormat("#.##0,00")).format($F{amount}.doubleValue())
It causes an exception and whan I try to use :
NumberFormat.getCurrencyInstance(Locale.ENGLISH).format($F{amount}.doubleValue())
I have the symbole of the currency which I don't want to print.
"#.##0,00" is an invalid format string.
The comma, and only the comma, is the grouping character, but it is localized, meaning if you set the Locale, you'll get the appropriate separator for your locale.
If you prefer the "one-line hack" way, this will work:
new DecimalFormat("#,##0.00").format($F{amount}.doubleValue()).replace(",", "x").replace(".", ",").replace("x", ".");
Well, I would use the DecimalFormatSymbols class to trick the formatter, like this:
public String formatNumber(double value) {
DecimalFormat formatter = new DecimalFormat("###,###.00");
DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.getDefault());
symbols.setDecimalSeparator(',');
symbols.setGroupingSeparator('.');
formatter.setDecimalFormatSymbols(symbols);
return formatter.format(value);
}
hope it works for you...
Maybe something like:
DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.ENGLISH);
return (new DecimalFormat("#.##0,00", symbols)).format($F{amount}.doubleValue());
Check out the two argument constructor for DecimalFormat