简体   繁体   English

Java用所有小数点设置双精度值,并且小数点后仅获得2位数字

[英]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. 在java中,我对存储和获得双​​重价值感到困惑。 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. 目前,我正在创建包含一些会计部分的软件,我想存储与计算出的值相同的计算值,例如202.234234234212312,而在显示时,我希望将其显示为小数点后2位的202.23。 and in calculation i want to do calculation with 2,4 or 6 digit. 在计算中,我想用2,4或6位数字进行计算。

for that i have 2 option 为此,我有2个选择

  1. Store value as it is calculated and in getter method of amount field, i can format it like. 存储值,因为它是计算得出的,并且在数量字段的getter方法中,我可以将其格式化。

     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. 我可以使用2个单独的字段进行获取和设置。

     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. 首先,所有涉及金钱的计算都应使用BigDecimal进行,而不是两倍。 A double cannot represent quantities like 0.1 exactly. 双精度不能精确表示0.1。

Secondly, your getAmountFormatted should not return a Double but a String if it is only intended for output purposes. 其次,如果getAmountFormatted仅用于输出目的, getAmountFormatted不应返回Double,而应返回String。

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: 如果我对您的理解正确,那么您只想为相同的值获取不同的格式,则可以只具有一个变量,而2个获取字段则如下所示:

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. 更重要的是,这不是您的工作,因为实体类的程序员要:1)预期将来需要格式化的值,以及2)不断更新实体类以提供您没有的新格式化值(不, 不能不会 )。

So just have the getAmount method return the raw unadulterated data: 因此,只需使getAmount方法返回原始的纯净数据即可:

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

} }

and eliminate getAmountFormatted from the entity. 并从实体中消除getAmountFormatted 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: 当您要显示格式化为两位小数的金额时,只需使用getAmount()获取原始金额并getAmount()其格式化:

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. 假设在您的实体创建时已包含第二个金额, otherAmount ,因为有人认为将来可能需要该金额。 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 . 并假设因为该字段当时没有计划使用,所以您不必费心创建getOtherAmountFormatted()方法来提供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 . 现在正在使用otherAmount字段,您需要以两个小数位和原始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: 是否 getOtherAmountFormatted()方法添加到实体类中 ,然后编写:

 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 . 实体不是最终需要otherAmount格式版本的otherAmount ,因此它不必仅仅为了容纳现在正好需要otherAmount格式版本的其他代码进行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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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