Fomat Double to 2 Decimal Places WITHOUT Using a String - java

I'm working on a project which requires me to have double values at exactly 2 decimal place. Using some math, I currently have most numbers rounded correctly, except if there are trailing zeros. I want to keep these 0's for results like 0.00 in particular This list is displayed in a TableView, so therefore must be sortable numerically. I'd like the double values to actually be doubles so that it may properly sort.
Is there any way I can keep 2 decimal places without String.format?
Thank you in advance.

You can use DecimalFormat
DecimalFormat df = new DecimalFormat("#.00");
00 = exactly two decimal places.
But this will give to you a String that you can convert again to a double, if you don't want to have this String-conversion check this question to see how it can be achieved using BigDecimal.
IMHO, if this is not for learning pourposes DecimalFormat will be enough for you. True, you will have to use String in some moment of the conversion, but you will only read and store double values and your sorting will be the correct...

This answer expands on the suggestion to use java.math.BigDecimal rather than double.
It is better for this application than double because every two decimal place number is exactly representable in BigDecimal. The rounding from an arbitrary double-representable value down to two decimal places can be easily done according to any of several rounding modes, including the one used for double arithmetic.
On the other hand, it is better than String because the natural sort order is numeric value.
Here is a short demo program illustrating these points:
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Test {
public static void main(String[] args) throws Throwable {
List<BigDecimal> list = new ArrayList<BigDecimal>();
double[] in = {
3.5,
Math.PI,
-100.123456,
1e6
};
System.out.println("Original doubles: "+Arrays.toString(in));
for(double d : in){
list.add(doubleToBigDecimal(d,1));
}
System.out.println("Before sorting: " + list);
Collections.sort(list);
System.out.println("After sorting: " + list);
}
public static BigDecimal doubleToBigDecimal(double in, int places) {
BigDecimal result = new BigDecimal(in).setScale(2,
BigDecimal.ROUND_HALF_EVEN);
return result;
}
}
Output:
Original doubles: [3.5, 3.141592653589793, -100.123456, 1000000.0]
Before sorting: [3.50, 3.14, -100.12, 1000000.00]
After sorting: [-100.12, 3.14, 3.50, 1000000.00]

You can use DecimalFormat, Just set format in DecimalFormat's constructor as your required 2dp so:
double d = 1.234567;
DecimalFormat df = new DecimalFormat("#.##");
System.out.print(df.format(d));
output:
1.23
this code will round the decimal upto to decimal places However it's result will be not upto 2 decimal if value it self is not minimum 2 decimal places. e.g.
double d = 1.2;
DecimalFormat df = new DecimalFormat("#.##");
System.out.print(df.format(d));
output:
1.2
as Thisaru Guruge said and Jordi Castilla answer you can use like:
double d = 1.2;
DecimalFormat df = new DecimalFormat("#.00");
System.out.print(df.format(d));
output:
1.20

You can try BigDecimal, or you can simply create your own object that implements the Comparable interface for sorting and has a toString() method that outputs the value formatted to two decimal places.
class TableValue implements Comparable<TableValue> {
double value;
public TableValue(double value) {
this.value = value;
}
public int compareTo(TableValue o) {
return Double.compare(this.value, o.value);
}
public String toString() {
DecimalFormat df = new DecimalFormat("0.00");
return df.format(value);
}
}

Doubles are imprecise, BigDecimal has an extra attribute: its precision.
So new BigDecimal("5.20") would have a precision of 2, whereas double 5.20 * 10000 probably sufficiently deviates from 52000.0.
Mind the avoidable new BigDecimal(5.20) neither knows a precision, neither is precise.
BigDecimal unfortunately has a COBOListic usage.

You can use DecimalFormat class upto how much precision point you want you can set in class constructor suppose you want 2 precision you can set like
Double total= 12.15356789;
DecimalFormat df = new DecimalFormat("##.00");
Format the value using format method
String dx = df.format(total);
This will return String value of your Double value if you want Double with formatted 2 precision then use below line of code
Double totalVal = Double.valueOf(df.format(total));
This will return you Double value with 2 precision output would be like 12.15

Related

Strange BigDecimal value using DecimalFormat

I'm trying to convert some string values in number using DecimalFormat. I try to explain you my problem in a better way:
I have the following method:
private BigDecimal loadBigDecimal(String value){
BigDecimal bigDecimalToReturn = null;
DecimalFormat df = new DecimalFormat("##.###");
bigDecimalToReturn = new BigDecimal(df.parse(value).doubleValue());
return bigDecimalToReturn;
}
Now if I try to run the method:
BigDeciaml dec = myObject.loadBigDecimal("120,11");
The value of dec is 120.1099999999999994315658113919198513031005859375.
Why decimalFormat is changing the scale of my value?
You are doing conversion to double and backwards. That's unnecessary and introduces rounding errors. You should use the following code:
private BigDecimal loadBigDecimal(String value) throws ParseException {
DecimalFormat df = new DecimalFormat("##.###");
df.setParseBigDecimal(true);
return (BigDecimal) df.parse(value);
}
Doubles are only approximations. That is correct for a double. If you want a specific scale, you need to tell it in the BigDecimal constructor.
That is because of the df.parse(value).doubleValue() call. At this point, the value is converted to a double.
double represent plus or minus the sum of powers of 2 (with positive and negative exponents).
One can write 120 as 64+32+16+8.
But one can't write 0.11 as a finite sum of power of 2.
So there is an approximation.
0.1099999999999994315658113919198513031005859375
Which is a sum of power of 2.
It's look like BigDecimal as a constructor with a string for parameter. Maybe you can just use it.
BigDecimal dec = new BigDecimal("120,11");

Formatting Double to exactly 2 Decimal places IF required

I require a DecimalFormat or a better equivalent of representing a Double value (in Java) which could be:
25 or 25.5
I need for that to be represented as either a whole number (25) or to two decimal places if it has any (25.50). This is because i'm printing it out as money.
I have the following format already:
DecimalFormat decFormat = new DecimalFormat("##,###.##");
This works perfectly if the Double is a whole number; I get the output $25,000. Except if the value is 25,000.5; it prints $25,000.5 when I need it to be printed as $25,000.50. The problem is as stated in the docs:
# a digit, zero shows as absent
So essentially the last zero is dropped off since it is optional.
I cannot do:
DecimalFormat decFormat = new DecimalFormat("##,###.#0");
as that is not allowed.
How can I achieve this?
Note:
These questions are related but do not cover what I need specifically with the DecimalFormat. Most of the answers suggest using a BigDecimal or printf. Is this the best thing to do? I don't have to use DecimalFormat but prefer to since i've started on that path (lots of code everywhere already using it).
Best way to Format a Double value to 2 Decimal places
How do I round a double to two decimal places in Java?
Round a double to 2 decimal places
This is definitely a bit of a hack, but I don't know if the DecimalFormat syntax allows for anything better. This simply checks to see if the number is real, and formats based on the spec you asked for.
double number = 25000.5;
DecimalFormat df;
if(number%1==0)
df = new DecimalFormat("##,###");
else
df = new DecimalFormat("##,###.00");
System.out.println(df.format(number));
When you need to return Decimal Format value this works
import java.text.DecimalFormat;
/**
* #return The weight of this brick in kg.
*/
public double getWeight()
{
DecimalFormat df = new DecimalFormat("#.##");
double number = ( getVolume() * WEIGHT_PER_CM3 ) / 1000;
//System.out.println(df.format(number));
return Double.valueOf ( df.format( number ) );
}

Regarding rounding off a value to certain decimal point

I was going through the class decimal format as I was trying format a decimal number in Java upto 2 decimal places or 3 decimal places.
I come up with this solution as shown below but please also let me know are there any other alternative that java provides us to achieve the same thing..!!
import java.text.DecimalFormat;
public class DecimalFormatExample {
public static void main(String args[]) {
//formatting numbers upto 2 decimal places in Java
DecimalFormat df = new DecimalFormat("#,###,##0.00");
System.out.println(df.format(364565.14));
System.out.println(df.format(364565.1454));
//formatting numbers upto 3 decimal places in Java
df = new DecimalFormat("#,###,##0.000");
System.out.println(df.format(364565.14));
System.out.println(df.format(364565.1454));
}
}
Output:
364,565.14
364,565.15
364,565.140
364,565.145
Please advise what are other alternatives that java provide us to achieve the same thing..!!
If you are bothered by re-defining your DecimalFormat, or if you suspect you'll be needing to do redefine many times, you could also do inline formatting with String.format(). Check the syntax for Formatter especially the Numeric sub-title.
Here is an alternative to round off...
double a = 123.564;
double roundOff = Math.round(a * 10.0) / 10.0;
System.out.println(roundOff);
roundOff = Math.round(a * 100.0) / 100.0;
System.out.println(roundOff);
The output is
123.6
123.56
Number of 0s while multiplying and dividing decides the rounding off.
Here is one method.
float round(float value, int roundUpTo){
float x=(float) Math.pow(10,roundUpTo);
value = value*x; // here you will guard your decimal points from loosing
value = Math.round(value) ; //this returns nearest int value
return (float) value/p;
}

Show decimal of a double only when needed

I got this problem with double (decimals).
When a double = 1.234567 Then I use String.format("%.3f", myString);
So the result is 1.234
But when my double is 10
The result will be 10,000
I want this to be 10
Is their a way to say that he only needs to show the decimals when it is "usefull"?
I saw some posts about this, but that was php or c#, couldn't find something for android/java about this (maybe I don't look good).
Hope you guys can help me out with this.
Edit, for now I use something like this: myString.replace(",000", "");
But I think their is a more "friendly" code for this.
The DecimalFormat with the # parameter is the way to go:
public static void main(String[] args) {
double d1 = 1.234567;
double d2 = 2;
NumberFormat nf = new DecimalFormat("##.###");
System.out.println(nf.format(d1));
System.out.println(nf.format(d2));
}
Will result in
1.235
2
Don't use doubles. You can lose some precision. Here's a general purpose function.
public static double round(double unrounded, int precision, int roundingMode)
{
BigDecimal bd = new BigDecimal(unrounded);
BigDecimal rounded = bd.setScale(precision, roundingMode);
return rounded.doubleValue();
}
You can call it with
round(yourNumber, 3, BigDecimal.ROUND_HALF_UP);
"precision" being the number of decimal points you desire.
Copy from Here.
Try it
double amount = 1.234567 ;
NumberFormat formatter = new DecimalFormat("##.###");
System.out.println("The Decimal Value is:"+formatter.format(amount));

How to save decimal in java

Having the following code in Java:
double operation = 890 / 1440;
System.out.println(operation);
Result:
0.0
What I want is to save the first 4 decimal digits of this operation (0.6180). Do you know how can I do it?
Initialize your variable with an expression that evaluates to a double rather than an int:
double operation = 890.0 / 1440.0;
Otherwise the expression is done using integer arithmetic (which ends up truncating the result). That truncated result then gets converted to a double.
You can use the double literal d - otherwise your numbers are considered of type int:
double operation = 890d / 1440d;
Then you can use a NumberFormat to specify the number of digits.
For example:
NumberFormat format = new DecimalFormat("#.####");
System.out.println(format.format(operation));
You can also do something like this:
double result = (double) 890 / 1400;
which prints the following:
0.6180555555555556
You can check how to round up the number here
This is done using BigDecimal
import java.math.BigDecimal;
import java.math.RoundingMode;
public class DecimalTest {
/**
* #param args
*/
public static void main(String[] args) {
double operation = 890.0 / 1440.0;
BigDecimal big = new BigDecimal(operation);
big = big.setScale(4, RoundingMode.HALF_UP);
double d2 = big.doubleValue();
System.out.println(String.format("operation : %s", operation));
System.out.println(String.format("scaled : %s", d2));
}
}
Output
operation : 0.6180555555555556
scaled : 0.6181
BigDecimal, although very clumsy to work with, gives some formatting options:
BigDecimal first = new BigDecimal(890);
BigDecimal second = new BigDecimal(1440);
System.out.println(first.divide(second, new MathContext(4, RoundingMode.HALF_EVEN)));
double operation = 890.0 / 1440;
System.out.printf(".4f\n", operation);
If you really want to round to the first 4 fractional digits you can also use integer arithmetic by first multiplying the first number so its digits are shifted the right amount f places to the left:
long fractionalPart = 10000L * 890L / 1440L;
I'm using long here to avoid any overflows in case the temporary result does not fit in 32 bits.

Categories