简体   繁体   中英

Java Set double value with all decimal point and get only 2 digit after decimal point

In java , I have confusion about store and get double value. Currently i am creating software where some accounting part included,i want to store calculated value same as they are calculated like 202.234234234212312 and while in display i want to display it as 202.23 in 2 digit after decimal point. and in calculation i want to do calculation with 2,4 or 6 digit.

for that i have 2 option

  1. Store value as it is calculated and in getter method of amount field, i can format it like.

     private Double amount; public Double getAmount() { DecimalFormat df2 = new DecimalFormat(".##"); return Double.valueOf(df2.format(amount)); } 

but problem with this method is , it will get formatted number at all time i get amount. i can't get actual stored amount.

  1. i can use 2 separate field for get and set.

     private Double amount; private Double amountFormatted; public Double getAmount() { return amount; } public Double getAmountFormatted() { DecimalFormat df2 = new DecimalFormat(".##"); return Double.valueOf(df2.format(amountFormatted)); } 

so please provide better way to store and get decimal value, Thank you.

First of all, all calculations involving money should be done in BigDecimal, not double. A double cannot represent quantities like 0.1 exactly.

Secondly, your getAmountFormatted should not return a Double but a String if it is only intended for output purposes.

Use the same amount variable for both methods.

If I understood you correctly, you just want to get different format for same value, you can just have one variable and 2 get fields like this:

private Double amount;

public Double getAmount() {
    return amount;
}

public Double getAmountFormatted() {
    DecimalFormat df2 = new DecimalFormat(".##");
    return Double.valueOf(df2.format(amount));
}

It's not an entity's job to provide formatted data for display: leave formatting to the code that is actually doing the displaying. More to the point, it's not your job as the programmer of the entity class to 1) anticipate every future need for a formatted value, and 2) constantly update the entity class to provide new formatted values that you didn't (nay, couldn't ) anticipate.

So just have the getAmount method return the raw unadulterated data:

public class Entity {
    private BigDecimal amount;
    public BigDecimal getAmount(){ 
        return amount;
    }
    //...

}

and eliminate getAmountFormatted from the entity. When you want to display the amount formatted to two decimal places, just obtain the raw amount with getAmount() and format it right on the spot:

Entity e = ...;
DecimalFormat df = new DecimalFormat(".##");
System.out.println("The amount is " + df.format(e.getAmount());

Here's a little story that illustrates why I think this is the way to go...

Suppose there's a second amount, otherAmount in your entity, that was included at the time the entity was created because someone thought it might be needed in the future. And suppose that, because the field had no planned use at the time, you didn't bother to create a getOtherAmountFormatted() method to provide a two-decimal-formatted version of otherAmount .

Now the future arrives. The otherAmount field is now being used, and you need to start displaying it with two decimal places along with the original amount . You know you're going to have to add the line

System.out.println("The other amount is " + ...

into the code that's displaying the amounts. Only question is, how do you finish that line. Do you add into your entity class a getOtherAmountFormatted() method and then write:

 System.out.println("The other amount is " + e.getOtherAmountFormatted());  // NO!!

Or do you not touch the entity class at all and just write:

 System.out.println("The other amount is " + df.format(e.getOtherAmount()); // Yes!!

The entity isn't the class that ultimately needs the formatted version of otherAmount , so it shouldn't have to change just to accommodate some entirely other bit of code that now happens to need a formatted version of otherAmount . Especially when that other bit of code can just get the unformatted amount, which the unmodified entity can already provide, and then do the needed formatting for itself.

Next scenario: in the further future, yet another requirement has come up to print those amount fields, but now with with three decimal places. Are you going to add yet more methods into the entity class to provide three-decimal versions of the amounts (in addition to the ones already providing two-decimal versions)? Or will you not touch the entity class at all and just write, at the point you need those three-decimal formatted values:

DecimalFormat df3 = new DecimalFormat(".###");
// ...
System.out.println("The amount in 3 decimals is " + df3.format(e.getAmount());
System.out.println("The other amount in 3 decimals is " + df3.format(e.getOtherAmount());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM