This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 3 years ago.
I want to convert the string to float with out losing it's precision.
String str = "140000.01";
float val = Float.valueOf(str);
Here, I'm getting the output i.e float value as 140000.02, is there any way to convert it into float without losing precision.
Simple way you can use double, like this:
String str = "140000.01";
double val = Double.parseDouble(str);
Related
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 4 years ago.
My problem while I am converting Double to BigDecimal I am loosing original value.
My requirement like input would be like Double val = 10435383974769502920d and want to convert like 10435.383974769502920 but getting below output.
I have tried with Double BigDecimal..but no success
Double val = 10435383974769502920d;
System.out.println(BigDecimal.valueOf(val).movePointLeft(15));
output : 10435.383974769502
Double val = 10435383974769502920d;
A double simply can't hold that many digits. Doubles are limited to ~15 decimal digits of precision. It's not BigDecimal that's losing the extra digits; it's the double you're starting with.
>>> System.out.printf("%f\n", 10435383974769502920d);
10435383974769502000.000000
Construct the BigDecimal with a string to avoid losing precision.
String val = "10435383974769502920";
System.out.println(new BigDecimal(val).movePointLeft(15));
This question already has answers here:
How do I print a double value without scientific notation using Java?
(18 answers)
Closed 5 years ago.
I want to print amount like 2523525252.025 when I using double data type it's changed in 2.523525252025E9 how can fix?
Example -
Double saleprice=145236.12;
int itemqountity=1500;
Double totalamount= saleprice*itemqountity;
Log.e("totalamount",""+totalamount);
output-
E/totalamount: 2.1785418E8
Please use DecimalFormat to print your value without scientific notation here is an example.
double test = 12345678;
DecimalFormat df = new DecimalFormat("#");
df.setMaximumFractionDigits(0);
System.out.println(df.format(test));
//12345678
BigDecimal bigDecimal = new BigDecimal(2.523525252025E9);
System.out.println(bigDecimal.toPlainString());
This question already has answers here:
How to convert float to int with Java
(9 answers)
Closed 7 years ago.
I need to convert a float number into an integer.
Can Java automatically convert float number into integers? If so, do normal rounding rules apply (e.g. 3.4 gets converted to 3, but 3.6 gets converted to 4)?
You have in Math library function round(float a) it's round the float to the nearest whole number.
int val = Math.round(3.6); \\ val = 4
int val2 = Math.round(3.4); \\ val2 = 3
It is a bit dirty but it works:
double a=3.6;
int b = (int) (a + 0.5);
See the result here
Math.round(3.6) would do that for you.
This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
What's the best practice to round a float to 2 decimals? [duplicate]
(7 answers)
Closed 9 years ago.
I've got a float value and i need to have only two decimals after comma. I'm using this code:
public static float getWhatINeed() {
StatFs statFs = new StatFs(Environment.getDataDirectory().getAbsolutePath());
float total =
((float)statFs.getBlockCount() * statFs.getBlockSize()) / (1073741824);
return total;
}
And it returns for example: 12.552425 in a textview. I need display something like: 12.55 that is enough for me. I saw this:
String s = String.format("%.2f", 1.2975118);
somewhere but I can't use it in my case because I use a float value and I need to use float. How could I solve this?
There is no mechanism to limit the number of decimal points in a float. A float is a float and it has an "unlimited" number of decimals. The String display of a float may be limited to a format only showing a specific number of decimals.
If you really NEED 2 decimals, use BigDecimal
You basically have 4 options:
return a float and deal with the fact that there are n decimal places
format to a String (which means a lot of string parsing if you need to do calculation)
convert to use BigDecimal
convert to use int and assume that the ones digit represents hundredths.
Did you try:
new DecimalFormat("00.00").format(1.2975118);
You can try as follows
DecimalFormat df = new DecimalFormat("0.##");
float a=1.256f;
System.out.println(df.format(a));
}
Out put
1.26
After setting precision and get as a String You can canvert it back to float by
float f = Float.parseFloat(YourString);
This question already has answers here:
How do I convert a String to an int in Java?
(47 answers)
How to do an Integer.parseInt() for a decimal number?
(10 answers)
Closed 9 years ago.
How to convert string value into int? I am getting number format exception.
String s = "20.00";
int i = (Integer.parseInt(s));
System.out.println(i);
Result should be like i=20.
What about:
int i = (int) Double.parseDouble(s);
Of course, "20.00" is not in a valid integer format.
String s = "20.00";
is not valid Integer value that is the reason its throwing NumberFormatException.
Format your number using either Double or Float then using narrow casting cast you number to int but you may loose precision if exists.
i.e. int I = (int) Double.parseDouble(str);