SQL Server: Data more fractional digits in Java - java

I'm importing data from MS SQL Server with Java and JDBC. The data in my database have 2 fractional digits.

The MONEY datatype in SQL Server has four digits of precision, so what you're seeing is what should be expected. You can use DECIMAL if you only want two digits of precision.
Your front end should be able to display the data however it wants to display it though.

Completely agree with the explanation from Tom H. If you want to format data on the user side you can use DecimalFormat or to be more precise as it is currency try using NumberFormat's currency instance.
DecimalFormat df = new DecimalFormat("##.00");
String result = df.format(Double.parseDouble((data)));
NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
String result = nf.format(new BigDecimal(data));
You should change the Locale according to your requirement. FYI, using currency instance will return value with corresponding currency sign from Locale.

Related

Java - NumberValue to String without Currency sign

I am trying to format a NumberValue as a String in the current locale format.
For example, for Locale.Germany these would be the results I am trying to get:
1 -> 1,00
1.1 -> 1,10
0.1 -> 0,10
What I have tried:
String test1 = NumberFormat.getInstance().format(1.1);
// -> 1,1
String test2 = NumberFormat.getCurrencyInstance().format(1.1);
// -> 1,10 €
How could I get the same format as the second example, without the currency symbol? Or alternativly, how could I make sure the result from the first example always has the amount of decimals needed for the current locale?
Edit: Since there are some currencies that have 0,2 or 3 decimals, I don't think setting the decimals to a fixed amount will have the correct results.
Also, some currencies have their symbol in the front, and some in the back, so cutting off the last characters will probably not work either.
http://apps.cybersource.com/library/documentation/sbc/quickref/currencies.pdf
java.util.Currency provides the necessary information. A reasonable approach to print currency values without the currency sign in pure Java code is to get a general-purpose NumberFormat instance and configure it to use the number of digits defined by the appropriate Currency object:
NumberFormat numberFormat = NumberFormat.getInstance(myLocale);
Currency currency = Currency.getInstance(myLocale);
numberFormat.setMinimumFractionDigits(currency.getDefaultFractionDigits());
If you need a specific currency as opposed to the default currency of the locale you use, use the String override of Currency.getInstance() instead and provide the appropriate currency code.

Currency symbol with another number format

I was wondering if there was an elegant way to set the currency format of a number in one way but keep the actual number formatting in another. It is Java. Essentially I am doing this
NumberFormat format = NumberFormat.getCurrencyInstance(locale);
This is largely fine except I am writing a system for UK based users and my users are upset that when showing (for example) euros the number is formatted as europeans would use it. So € 500,000 as a UK person would write it is displaying as € 500.000 (i.e. swap , for .). I was going to swap locale for locale.UK but then I will have the wrong currency symbol!
I have a couple of dirty fixes for this but I wondered if there was an elegant way to keep the currency symbol of the locale with the local locale (locale.UK) number format.
Different currencies can also place the currency symbol before or after the string, or have a different number of decimal places (if any). It is not really clear from your question how you want to handle those cases, but assuming you want to preserve those differences, try this.
Instead of just swapping in the currency symbol into your local number format, you could start with the foreign format and substitute the decimal format symbols with your local version. Those also include the currency, so you have to swap that back (don't worry, it's a copy).
public static NumberFormat localStyleForeignFormat(Locale locale) {
NumberFormat format = NumberFormat.getCurrencyInstance(locale);
if (format instanceof DecimalFormat) {
DecimalFormat df = (DecimalFormat) format;
// use local/default decimal symbols with original currency symbol
DecimalFormatSymbols dfs = new DecimalFormat().getDecimalFormatSymbols();
dfs.setCurrency(df.getCurrency());
df.setDecimalFormatSymbols(dfs);
}
return format;
}
This way, you also retain the correct positioning of the currency symbol and the number of decimal places. Some examples, for default-Locale Locale.UK
en_GB £500,000.00 £500,000.00
fr_FR 500 000,00 € 500,000.00 €
it_IT € 500.000,00 € 500,000.00
ja_JP ¥500,000 JPY500,000
hi_IN रू ५००,०००.०० INR 500,000.00
If you also want to preserve the foreign currency symbol, instead of the local equivalent, use
localDfs.setCurrencySymbol(df.getCurrency().getSymbol(locale));
You can specify the currency symbol on the NumberFormat with the setCurrency method.
Then simply use the Locale.UK to have the proper grouping separator displayed.
format.setCurrency(Currency.getInstance("EUR"));
Note that for a better handling of the grouping/decimal separator you might want to use a DecimalFormat instead.
DecimalFormatSymbols custom=new DecimalFormatSymbols();
custom.setDecimalSeparator('.');
custom.setGroupingSeparator(',');
DecimalFormat format = DecimalFormat.getInstance();
format.setDecimalFormatSymbols(custom);
format.setCurrency(Currency.getInstance("EUR"));
Then specify the correct pattern, example "€ ###,###.00".

Using Default Locale Separator with NumberFormat, and then rounding number

I have this Java code:
NumberFormat nf = NumberFormat.getNumberInstance(Locale.getDefault());
DecimalFormat df = (DecimalFormat)nf;
newCurr = df.format(dCurr);
Basically, I pass in a number, say 12.344.
I want it rounded two places AND to use the Locale's default separator (either "." or ","). So, for example in some countries in Europe, I want this to be 12,34
So far with code above, I am halfway there. I get 12,344. I can't find where to place the DecimalFormat of ("#.##") so it can be rounded.
In other words, I can I incorporate DecimalFormat df=new DecimalFormat("#.##"); in the above? or do I have to find another way?
Edit: I am thinking I have to do the old way of (100.00 * var)/ 100.00 and pass that in?
The method setMaximumFractionDigit will do the work. See the rest of the available methods: http://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html#setMaximumFractionDigits%28int%29

NumberFormatException on European Versions of Android?

I have an app which runs the following two lines of code upon starting:
DecimalFormat decim = new DecimalFormat("#.00");
return Double.parseDouble(decim.format(totalNumberOfCredits));
When I start the app on my American phone, the value of decim.format(totalNumberOfCredits) is .00.
However, in my Google Play Developer Console, I have a dozen crashes, all of which look like this:
Caused by: java.lang.NumberFormatException: Invalid double: ",00"
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.parseDouble(StringToReal.java:269)
at java.lang.Double.parseDouble(Double.java:295)
Is it really possible that DecimalFormat is producing a comma version of the decimal on European phones?
Is it really possible that DecimalFormat is producing a comma version of the decimal on European phones?
Yes, absolutely. That's what it's meant to do, after all:
Creates a DecimalFormat using the given pattern and the symbols for the default locale. This is a convenient way to obtain a DecimalFormat when internationalization is not the main concern.
To obtain standard formats for a given locale, use the factory methods on NumberFormat such as getNumberInstance. These factories will return the most appropriate sub-class of NumberFormat for a given locale.
Note that this isn't a matter of a "European version of Android" - it's just a matter of using Android in a context where the default locale uses , as the decimal separator.
If you want to use the symbols for a particular locale, but using a specific pattern, you could use:
DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(Locale.US);
DecimalFormat format = new DecimalFormat("#.00", symbols);
Having said that, it's not at all clear what you're trying to do in the first place - why would you format and then parse a number? You should almost always avoid string conversions when you don't really need them. Why not just convert it directly? (We don't know what totalNumberOfCredits is, which doesn't help.)
public double getTwoPointDecimal(double value) {
DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.US);
return Double.parseDouble(new DecimalFormat("##.##", symbols).format(value));
}
try it, its help me in my project
double unit = Float.parseFloat(String);
DecimalFormat decimal = new DecimalFormat("##.###").format(unit);
try this it help me in my project

Java Decimal Format - as much precision as given

I'm working with DecimalFormat, I want to be able to read and write decimals with as much precision as given (I'm converting to BigDecimal).
Essentially, I want a DecimalFormat which enforces the following pattern "\d+(\.\d+)?" i.e. "at least one digit then, optionally, a decimal separator followed by at least one digit".
I'm struggling to be able to implement this using DecimalFormat, I've tried several patterns but they seem to enforced fixed number of digits.
I'm open to alternative ways of achieving this too.
Edit:
For a little more background, I'm parsing user-supplied data in which decimals could be formatted in any way, and possibly not in the locale format. I'm hoping to let them supply a decimal format string which I can use the parse the data.
Since you noted in a comment that you need Locale support:
Locale locale = //get this from somewhere else
DecimalFormat df = new DecimalFormat();
df.setDecimalFormatSymbols(new DecimalFormatSymbols(locale));
df.setMaximumFractionDigits(Integer.MAX_VALUE);
df.setMinimumFractionDigits(1);
df.setParseBigDecimal(true);
And then parse.
This seems to work fine:
public static void main(String[] args) throws Exception{
DecimalFormat f = new DecimalFormat("0.#");
f.setParseBigDecimal(true);
f.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US));// if required
System.out.println(f.parse("1.0")); // 1.0
System.out.println(f.parse("1")); // 1
System.out.println(f.parse("1.1")); // 1.1
System.out.println(f.parse("1.123")); // 1.123
System.out.println(f.parse("1.")); // 1
System.out.println(f.parse(".01")); // 0.01
}
Except for the last two that violate your "at least one digit" requirement. You may have to check that separately using a regex if it's really important.

Categories

Resources