What can I do against floating point notations Errors? [duplicate] - java

This question already has answers here:
Why are floating point numbers inaccurate?
(5 answers)
Why not use Double or Float to represent currency?
(16 answers)
Closed 5 years ago.
I need to multiply a float by 100 in order to convert from € to cents. The problem I am having is, that the value of big numbers isn't calculated correctly.
For example:
String aa = "1000009.00";
aa = aa.replaceAll(",", ".");
float bb = Float.parseFloat(aa);
bb=Math.round(bb*100);
System.out.println(bb);
What I am getting is: 1.00000896E8
So I know this is because of the way float in Java works.
I have searched for an answer but people always say "use Math.round()" which doesn't work for me.
What can i do to prevent this?

You can use double for more precision (or BigDecimal if you expect to work with very big numbers).
String aa = "1000009.00";
double bb = Double.parseDouble(aa);
bb=Math.round(bb*100);
System.out.printf("%.2f", bb); // it prints only two digits after the decimal point
Output
100000900.00

You can use BigDecimal::multiply for example :
String aa = "1000009.00";
aa = aa.replaceAll(",", ".");
BigDecimal fullValue = new BigDecimal(aa);
System.out.println("full value = " + fullValue.multiply(new BigDecimal(100)));
Output
full value = 100000900.00

Related

Printing out double with variable length after decimal point [duplicate]

This question already has answers here:
Use DecimalFormat to get varying amount of decimal places
(4 answers)
Closed 1 year ago.
I was searching for a solution of how to print double with variable length. Means: user will define how many digits he wants after the decimal point, but without success.
I've come to something like, but it doesn't work :
num - double
dec(length) - integer
System.out.printf("%.(%d)f\n", num, dec);
Are you looking for something like the following?
Maybe solution 2 is suitable for you.
Solution 1:
System.out.printf("%.2f", val); // "%.2f" it's a string so you can make it in several ways...eg: "%."+ dec + "f";
Solution 2:
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2); // you can use int variable instead of 2.. eg: df.setMaximumFractionDigits(dec);
System.out.println(df.format(decimalNumber));

how to check if two doubles are equal in java [duplicate]

This question already has answers here:
What is good way to check equality between 2 doubles in Java
(6 answers)
Closed 3 years ago.
I am writing a program to help students practice division, how can I compare the user Input and the correct answer if they are doubles
You can convert it to BigDecimal, and compare with the required precision of decimal places
BigDecimal aa = new BigDecimal(a);
BigDecimal bb = new BigDecimal(b);
aa = aa.setScale(4, BigDecimal.ROUND_DOWN);
bb = bb.setScale(4, BigDecimal.ROUND_DOWN);
return aa.equals(bb);
Either use
Double.compare()
or
final double EPSILON = 0.00000001d;
Math.abs(firstDouble - secondDouble) < EPSILON;
it's based on level of accuracy you needed for comparison

How to format a Double coming from a Object in Java? [duplicate]

This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 6 years ago.
I want to format a Double obtained from an Object so it only displays three digits after the decimal point. Here's the current code:
Three a = Data.get(index);
// Get the y-axis acceleration value
double b = a.getY();
String accelerationOutUnfiltered = Double.toString(b);
Data[0] = accelerationOutUnfiltered;
Note: I am doing this in Android, and when I use String.format("%.3f", y) this doesn't work and it throws me a error in Android Studio. Currently the above code works but it displays 15 digits after the decimal point.
I have tried several forms, but they all have failed. Please help. Thanks :)
You could use DecimalFormatter.
For example:
double b = a.getY();
DecimalFormat formatter = new DecimalFormat("#.###");
formatter.setRoundingMode(RoundingMode.CEILING);
String formattedDouble = formatter.format(b);
Have tried decimal formatter?
You do something like this:
DecimalFormat df = new DecimalFormat("#.#####");
df.format(<your_number>);

How to change a float into a String in Java? [duplicate]

This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 7 years ago.
Is it possible to change a float value to a String? If possible, is it also possible to converting it to a String while rounding the number to the nearest integer?
For example if I have a float such as 2.335 then, can I change it to a String of value "2.335" or "2" (by rounding it)?
Use java Float class:
String s = Float.toString(25.0f);
if you want to round down a number, simply use the Math.floor() function.
float f = 2.9999f;
String s = Float.toString(Math.floor(f));//rounds the number to 2 and converts to String
first line rounds the number down to the nearest integer and the second line converts it to a string.
Another way of doing this is using the String.valueOf(floatNumber);
float amount=100.00f;
String strAmount=String.valueOf(amount);
To do this you can simply do
float example = 2.335
String s = String.valueOf(Math.round(example));
To convert a float to a String:
String s = Float.toString(2.335f);
Rounding can be done via
String.format("%.5g%n", 0.912385);
which returns 0.91239
For a more elaborate answer, see Round a number in Java
Your first requirement can be fullfilled with String.valueOf
float f = 1.6f;
String str = String.valueOf(f);
For roundoff you can use Math.round and not Math.floor. As Math.floor will convert 1.6 to 1.0 and not 2.0 while Math.round will roundoff your number to nearest integer.

Remove scientific notation on Float string with lots of trailing 0's [duplicate]

This question already has answers here:
Formatting a String to Remove Scientific Notation - Java
(4 answers)
Closed 9 years ago.
I'm trying to convert a HEX string into float from a data that comes from a device:
The device output in the LCD display,
0x00ac and the corresponding float value is 5.06
The method that calculated the value is:
final byte[] temp = new byte[1];
temp[0] = ba[0];
float fff = hexToFloat(bytesToHex(temp)).floatValue();
final float ff = ( fff / 42) * 1000;
String floatString = Float.toString(ff);
However the floatString output string contains "E-" notation. I need to remove this. Also it seems that the value of ff is slightly different from what the device output in the LCD.
I don't see how 0x00ac can be equal to 5.06, but here is how to get rid of the scientific notation with BigDecimals:
BigDecimal num = new BigDecimal(fltInput);
String numWithNoExponents = num.toPlainString()

Categories