gpt4 book ai didi

java - Android/Java - 如何知道 DecimalFormat 的当前值是多少

转载 作者:行者123 更新时间:2023-12-02 05:50:22 26 4
gpt4 key购买 nike

我有一个按钮和 TextView。在 TextView 中,我显示一个 float 的值,点后有两位数字。

每次按下按钮,当前 float (显示在 TextView 上)都会增加 0.01。

为此,我使用了下一个代码 -

        float hightCurrent =  Float.parseFloat(hightNum.getText().toString());
hightCurrent -= 0.01;
DecimalFormat myFormatter = new DecimalFormat("##0.00");
myFormatter.format(hightCurrent);

if(myFormatter.equals("1.10")){
Log.d("GOT", "Got in");

}else{

}


hightNum.setText((myFormatter.format(hightCurrent)));

现在,如您所见,我有一个 if 语句。我想知道的是当该值为 1.10 时。

问题是,当显示的值是 1.10 时,它肯定不会进入 if 语句。

那么我怎样才能让这段代码工作呢?我如何知道该值何时为 1.10?

感谢您的帮助

最佳答案

  1. myFormatter.format(hightCurrent); 分配给字符串变量
  2. 比较此变量的值

因此;

String str = myFormatter.format(hightCurrent);
if(str.equals("1.10")){
...

关于java - Android/Java - 如何知道 DecimalFormat 的当前值是多少,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23595138/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com