I want to parse an integer accurately, one that has been potentially formatted according to the current locale. If I didn't parse the integer accurately, I want to know it. So I use:
String string = "1111122222333334444455555";
Locale locale = Locale.getDefault();
NumberFormat numberFormat = NumberFormat.getIntegerInstance(locale);
numberFormat.setParseIntegerOnly();
Number number = numberFormat.parse(string);
Obviously "1111122222333334444455555" represents a big number, bigger than a Long can handle. So NumberFormat gives me... a Double??
I guess I would have expected to receive a BigInteger rather than a Double, especially since I asked for an integer-specific number formatter. But never mind that; the bigger problem is that the double value I get back is 1.1111222223333344E24! This is not equal to 1111122222333334444455555!!
If NumberFormat gives me a parsed value that does not equal that stored in the input string, how do I detect that?
Put another way: "How can I know if the Double value I get back from NumberFormat is exactly equivalent to the integral value represented in the original string?"
The javadocs for parse() state that it will return a Long if possible, otherwise it will return a Double. So just check that the return value is a Long.
"Returns a Long if possible (e.g., within the range [Long.MIN_VALUE, Long.MAX_VALUE] and with no decimals), otherwise a Double."
"How can I know if the Double value I get back from NumberFormat is exactly equivalent to the integral value represented in the original string?"
If it returns a Double, then it is not exactly equivalent to your integral value because a Double cannot accurately represent values at that magnitude. Concrete example:
Number a = numberFormat.parse("-9223372036854775809"); // Integer.MIN_VALUE - 1
Number b = numberFormat.parse("-9223372036854775810"); // Integer.MIN_VALUE - 2
System.out.println((a.equals(b))); // prints "true"
Number c = numberFormat.parse("-9223372036854776800");
System.out.println((a.equals(c))); // prints "true"
For you question -
If NumberFormat gives me a parsed value that does not equal that stored in the input string, how do I detect that?
You can use
if(number.toString().equals(string))
//Parsed correctly
else
//Invalid parse
This might not be the solution but worth notice.
public static void main(String[] args) {
String string = "1111122222333334444455555";
Locale locale = Locale.getDefault();
NumberFormat numberFormat = NumberFormat.getIntegerInstance(locale);
numberFormat.setParseIntegerOnly(true);
Number number = numberFormat.parse(string);
BigDecimal b = new BigDecimal(number.toString());
System.out.println(b.toBigInteger());
}
Output of this code is : 1111122222333334400000000
As you can see this is not equal to the number in the actual string , so their might be an overflow occoured.
Related
How can I get an Integer to be two digits. If it's under 10 it would show 01,02, etc. I want to keep the Integer as an Integer. String.format gives a string. How can I do this with Decimalformat for ex
Decimalformat df = new Decimalformat(???);
If you want to keep the value as an int or Integer, there's nothing to be done here. A number doesn't have a format - it just has a value. For example, an int isn't "in decimal" or "in hex" or "in binary" - if you write
int x = 16;
int y = 0x10;
those are the exact same values.
The idea of "padding" only makes any sense when it comes to a textual representation of the number - which is when you end up with a string.
you do something likewise, but directly you can't get into int or Integer
NumberFormat formatter = new DecimalFormat("00");
String s = formatter.format(1); // ----> 01
I am trying to convert String value to BigDecimal value.
when i use 0.000001234 I am getting same value in BigDecimal. But, when I use 0.000000123 or 0.000000012 I am getting 1.23E-7 or 1.2E-8.
I need like below,
if input is String = 0.000000123
Then output should be,
BigDecimal = 0.000000123
Please help me
import java.math.BigDecimal;
public class DecimalEx {
public static void main(String args[]){
String s = "0.000000023";
BigDecimal big = new BigDecimal(s);
System.out.println(big);
}
}
You're converting the String to a BigDecimal with no problems. The BigDecimal value has no notion of whether the value has exponential notation or not... it's just a scaled value.
The problem you're seeing is when you implicitly call BigDecimal.toString(), which is documented to use exponential notation sometimes:
If the scale is greater than or equal to zero and the adjusted exponent is greater than or equal to -6, the number will be converted to a character form without using exponential notation.
Instead, you want to use BigDecimal.toPlainString():
Returns a string representation of this BigDecimal without an exponent field.
Just change your last line of code to:
System.out.println(big.toPlainString());
I have a problem where my 999999999.9999999999 String is being rounded to 1,000,000,000 BigDecimal when I parse it.
Code:
NumberFormat format = new DecimalFormat("#.0000000000######");
format.setParseIntegerOnly(false);
Number number = format.parse(text);
Result:
number = 1.0E9
If I use the same format to parse the result BigDecimal back to String:
format.format(number) results in 1,000,000,000
Why does this happen and how I can force the original format.parse(text) call not to round?
Unless there's some code you've omitted, parse is returning a Double, not a BigDecimal. (Try System.out.println(number.getClass()) to check this.)
If you want a BigDecimal, you have to use setParseBigDecimal to tell it to return a BigDecimal. (And to do that, format has to be declared as DecimalFormat.)
DecimalFormat format = new DecimalFormat("#.0000000000######");
format.setParseBigDecimal(true);
Number number = format.parse(text);
I have following sample (link to ideone).
long lDurationMillis = 0;
lDurationMillis = Long.parseLong("30000.1");
System.out.print("Play Duration:" + lDurationMillis);
It throws an exception:
Exception in thread "main" java.lang.NumberFormatException: For input string: "30000.1"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Long.parseLong(Long.java:419)
at java.lang.Long.parseLong(Long.java:468)
at Main.main(Main.java:9)
But why it wont let me convert that number to a string directly ?I can convert number to integer and than convert to double . But is there any other way ?
The value 30000.1 is an invalid long value. You could parse the double value first:
lDurationMillis = (long)Double.parseDouble("30000.1");
You could use BigDecimal in this case:
BigDecimal bd = new BigDecimal("30000.1");
long l = bd.setScale(0, BigDecimal.ROUND_HALF_UP).longValue();
System.out.println(l);
The title says converting string to long, first question is about coverting number to string, next statement about converting number to integer to string. I am confuse.
But for anything to do with floating points, I have to point you at obligatory reference What Every Computer Scientist Should Know About Floating-Point Arithmetic .
In java, int and long do not have fractional parts, so a string like 3000.1 cannot be covnerted to one of these. It can be converted to float or double but if you read the above article you will realize that the coversion can be lossy, i.e. if you canvert that double back to a String you may not get the original 3000.1 back. It will be something close, for appropriate defintion of close, but may not be same.
If you want to use exact precision then BigDecimal is your friend. It will be much slower then the number types, but it will be precise.
Because long can't have fractional part, you could convert it to double and then cast it to long ignoring fractional part
You can do NumberFormat handling as below :
long lDurationMillis = 0;
try{
NumberFormat nf = NumberFormat.getInstance();
lDurationMillis = nf.parse("30000.1").longValue();
System.out.print("Play Duration:" + lDurationMillis);
}catch(ParseException e)
{
e.printStackTrace();
}
Output:
Play Duration:30000
The default Java Float.toString(float) method prints a floating point number with "only as many ... [fractional] digits as are needed to uniquely distinguish the argument value from adjacent values of type float." Perfect! Just what I need---except that I need to do this in a locale-specific way (e.g. on a French computer "1.23" would be represented as "1,23").
How do I reproduce the functionality of Float.toString(float) in a locale-aware manner?
You can try the following:
String s1 = String.format(Locale.FRANCE, "%.2f", 1.23f); // Country FRANCE 1,23
String s2 = String.format(Locale.FRENCH, "%.2f", 1.23f); // Language FRENCH 1,23
String s3 = String.format("%.2f", 1.23f); // Default Locale
.2 is optional and is the number of fractional digits
May works fine with the third if Locale.getDefault() return the appropriate Locale for France.
Another way is using NumberFormat
NumberFormat nf = NumberFormat.getInstance(Locale.FRENCH);
String s4 = nf.format(1.23f); // 1,23
I think that the important part of the question is that Float.toString() automatically figures out the number of decimal places that is relevant, and does a pretty good job of it. In my JRE, the class sun.misc.FloatingDecimal class is used. The logic in that class looks pretty complex and hard to reproduce.
Another class that can be used to guess the correct number of digits to display is BigDecimal. For example, if you are working with double rather than float:
double d = 1.23;
NumberFormat nf = NumberFormat.getNumberInstance(Locale.FRENCH);
BigDecimal bd = BigDecimal.valueOf(d);
nf.setMaximumFractionDigits(bd.scale());
String s = nf.format(1.23f); // 1,23
For floats, you first have to figure out how to convert from float to BigDecimal without changing the precision: How to convert from float to bigDecimal in java?