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€.
Related
I have a method that convert a number into a currency, for "USD" and "GBP" it's working good, but with "EUR" the NumberFormat it's rendering a string with a space between the symbol and the number € 1.207.987,00 rather then dollar and pound "$1,207,987.00", "£1,207,987.00". I tried use replace and replace all to remove this but nothing works for me, follow the code:
public static void main(String[] argsd) {
Number rawNumber = 120798700;
NumberFormat numberFormat = NumberFormat.getCurrencyInstance(Locale.forLanguageTag("nl"));
Currency currency = Currency.getInstance("EUR");
numberFormat.setCurrency(currency);
String numberRemoveSpaces = numberFormat.format((rawNumber.floatValue() / 100)).replaceAll("\\s+", "");
System.out.println(numberRemoveSpaces);
}
If you want to remove it, try this:
String numberRemoveSpaces = numberFormat.format((rawNumber.floatValue() / 100)).replaceAll("\\p{Z}","");
That removes any kind of whitespace or invisible separator.
You can use the following, instead of your current replaceAll():
replaceFirst("\\u00A0", "")
The Unicode value of U+00A0 is a non-breaking space (see here). This is the specific character being used to separate the currency symbol from the amount.
You can also choose to build a custom format as follows:
NumberFormat nf = NumberFormat.getCurrencyInstance();
DecimalFormatSymbols dfs = new DecimalFormatSymbols();
dfs.setCurrencySymbol("€");
dfs.setGroupingSeparator('.');
dfs.setMonetaryDecimalSeparator(',');
((DecimalFormat) nf).setDecimalFormatSymbols(dfs);
System.out.println(nf.format(rawNumber.floatValue() / 100));
This also gives the same output:
€1.207.987,00
Thanks guys for all the answers but according with http://www.bubblefoundry.com/blog/2013/11/formatting-dutch-currency-amounts/ the default format for Netherlands currency have the space between the symbol and number, so i'll keep with the space.
My question is about of toString() and toPlainString() methods of the BigDecimal dataTypewhich produces the output like
750.0000
150.0000
... etc
My question is how to specify the number of zeros followed after the dot? Is there a way to do it instead of String.replace(".0000", ".00") method?
Use DecimalFormat in combination with DecimalFormatSymbols:
DecimalFormatSymbols dfs = new DecimalFormatSymbols();
dfs.setDecimalSeparator('.');
DecimalFormat df = new DecimalFormat("##.##");
df.setDecimalFormatSymbols(dfs);
df.format(myNumber)
Without using DecimalFormatSymbols you would end up with a comma as a decimal seperator instead.
Please use the below code.
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(2);
String s = nf.format(1111.2222);
System.out.println(s);
Apart from decimal format you can also use setScale(2) like this
new BigDecimal("1.0000").setScale(2)
Also setScale allows you can specify the Rounding Mode
You could use setScale method and optinally you could choose rounding methodology of your own. Somethign like:
BigDecimal b = new BigDecimal("750.0000");
b.setScale(2);
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();
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)
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