I have a string similar to: 7.6E+7.
My question is simple: How do I turn this into its corresponding number: 76000000?
I have tried using substring to isolate the E+7 part, then parse the 7 part, then move the decimal places over 7. Is there an easier way to do this?
Thank you!
long n = Double.valueOf("7.6E+7").longValue();
System.out.println(d);
// prints 76000000 to the output.
I suggest using Double.parseDouble():
double val = Double.parseDouble(str);
where str is the input string.
You can use Double.parseDouble() to get it as a number.
String e = "7.6E+7";
System.out.println(Double.parseDouble(e));
Giving the output 7.6E7. If you do not want the E in the output you can use
NumberFormat f = NumberFormat.getInstance();
f.setGroupingUsed(false);
System.out.println(f.format(Double.parseDouble(e)));
Which will give you the output 76000000 without casting to a whole number. Eg adding 0.1 to the number will give the output 76000000.1
If you are sure the number can ultimately be cast into an integer without losing precision than alternatively you could do:
int d = (int) Double.parseDouble("7.6E+7");
System.out.println(d);
Which prints 76000000 to the output.
Related
I need to convert some floats to format like this: 2,5000E-003, 2,8625E+000
I know i can do it like this:
String.format("%s,%sE%s", digitInt, digitAfterDot, digitDegree)
But I hope somebody knows more clever and natty solution
You can use:
String.format("%1.4e",12345.67890123)
The result of this is:
1,2346e+04
Here is some documentation
EDIT
If you need 3 digits in the exponent, you can use the DecimalFormat Formatter:
DecimalFormat format = new DecimalFormat("0.0000E000");
System.out.print(format.format(12345.67890123f));
Which outputs:
1,2346E004
Note that it does not come with a positive sign if the exponent is positive.
You can fix this with:
DecimalFormat format = new DecimalFormat("0.0000E000");
String result = format.format(12345.67890123f);
if (!result.contains("E-")) {
result = result.replace("E", "E+");
}
I have a number as
Double d = 100.000000;
I want to remove the decimal point and print the values as 100000000
(Note I am using java)
It is impossible. double doesn't store zeroes after decimal point so 1.0000 is equal to 1.0.
Hint: you can use BigDecimal for this. It have scale.
I'm afraid 100.000000 does not equal 100000000 and as mentioned by #talex, double doesn't store the zeros after the decimal point.
Your best bet is to use a String and remove the . manually:
String s = "100.000000";
System.out.println(s.replaceAll("\\.", "")); //note '.' needs to be escaped
Output:
100000000
You could parse it as a Double then if necessary.
Format the value using String.format and the remove the separator.
double d = 100.000;
String formatted = String.format(
Locale.US, //Using a Locale US to be sure the decimal separator is a "."
"%5f", //A decimal value with 5decimal
d) //The value to format
.replace(".", ""); //remove the separator
System.out.println(formatted);
100000000
Other examples :
100.000123456 > 100000123
You can see that the value is truncated, it is important to understand that.
Note that I have set the String to have 5 decimal number, but this up to you.
the double does not store the number as 100.0000 it just stored as 100.0 that means any unnecessary zeros on the right will be deleted but if the number was like this 100.01234 u can use this trick
Double d = 100.01245;
String text = Double.toString(d);
text.replace(".","");
d = Double.parseDouble(text);
or u can store the number as sting from the beginning
String text = "100.000000";
d.replace(".","");
double d = Double.parseDouble(text);
I have a simple question, but it's rather difficult to google. I have an input field for a number, and I need to make this number into a currency. So, if a user inputs 120, I need to format that into $1.20. Then, if they add another digit, say the number becomes 1204, I need to format this as $12.04. I'm using a Double.ParseDouble, but for say 120, this yields $120.00. So, I guess I need something like ParseDouble that will turn a value like 120 into $1.20 instead of $120.00. How do I do this?
use Double.parseDouble(double number);, but easily multiply with 0.01 :)
Then you have your currency and everything is fine ;)
Devlen
You can do it without even parsing in a numeric.
Use a StringBuilder object to do so:
String input = "120";
String output = new StringBuilder(input).insert(input.length() - 2,".")
.insert(0, "$").toString();
System.out.println(output); // Prints $1.20
public String getNumberCurrency(double number){
NumberFormat formatter = NumberFormat.getCurrencyInstance(Locale.getDefault());
String moneyString = formatter.format(number/100);
return moneyString;
}
I was wondering if there is an existing method to convert a formatted number String to number, such as "123,456.78" to 123456.78
Basically, unlike DecimalFormat function, which turns a double variable to a String following that a given format such as "###,###.##" pattern. I want to implement a reverse of this functionality, which turns a String with "###,###.##" format to a double. Is there APIs to do this?
Thank you.
You should have looked through the documentation for DecimalFormat and its superclass. You would have discovered that it has not only format methods, but also parse methods like this one.
The easiest way to do what you want is:
NumberFormat format = NumberFormat.getInstance();
Number value = format.parse(string);
// If you specifically want a double...
double d = value.doubleValue();
You will have to catch ParseException and deal with it. How you do that depends on what you want to do when your string does not represent a valid numeric value. If it's user input, you may want to ask the user to enter the text again.
Here is a simple way to do this
String number = "20,000,000";
int x = Integer.parseInt(number.replace(",", ""));
System.out.println(x);
You just replace the char's that not belong to a number with "" and then parse it into a primitive.
String number = "20,000,000.56";
DecimalFormat df = new DecimalFormat("#");
df.setMaximumFractionDigits(5);
double x = Double.parseDouble(number.replace(",", ""));
System.out.println(df.format(x));
It is a bit different for a Double cause it will display the exponential output and you'll have to prevent that. The code above does that.
df.format(x)
Returns a String but you can cast it with the Double.parseDouble method
Here's a method using a Regex and the replace method if you have more than one delimiter and you know them all :
Let's say the delimiters here are "-" and ","
double x = Double.parseDouble(number.replace("[-,]", "");
I have value like this:
String x = "10,000";
I want to convert this to int.
I can convert it by removing comma like below:
String y = x.replace(",", "");
int value1 = Integer.parseInt(y);
But I don't want to do it like above.
Any other suggestions like inbuilt function? Or any other recommended ways for this?
You can simply:
NumberFormat.getNumberInstance(Locale.UK).parse(x);
Read about:
NumberFormat
Locale.UK and others.
Try this:
NumberFormat format = NumberFormat.getInstance(Locale.US);
Number number = format.parse("10,000");
// Now you can get number values from the object (like int, long, double)
System.out.println(number.intValue());
Output:
10000
Note: If you string contains values after decimal point, you need to use number.doubleValue to retain the precision because number.intValue() will simply ignore values after decimal points.
Use any one of these:
NumberFormat.getNumberInstance(Locale.ENGLISH).parse("10,000").intValue();
NumberFormat.getNumberInstance(Locale.US).parse("10,000").intValue();
NumberFormat.getNumberInstance(Locale.UK).parse("10,000").intValue();