【问题标题】:Android - Two decimal points [duplicate]Android - 两位小数[重复]
【发布时间】:2020-07-10 10:36:50
【问题描述】:

我正在创建一个货币转换应用程序。我想得到小数点后两位的答案。

这是我的代码:-

public class MainActivity extends AppCompatActivity {

    public void pri (View view){

        EditText amount = findViewById(R.id.amount);

        Double amout =  Double.parseDouble(amount.getText().toString());

        Double samount = amout * 180;


        Toast.makeText(MainActivity.this, "S. " + rsamount.toString(), Toast.LENGTH_SHORT).show();

        Log.i("price", amount.getText().toString());

    }

谢谢!

【问题讨论】:

  • 那不是 C,那是 Java。
  • 试试这个。 DecimalFormat df = new DecimalFormat("#.##");双 samount = df.format(amout * 180);

标签: java android


【解决方案1】:

这是kotlin中将数字转换为两位小数的函数。

fun roundOffDecimal(number: Double): Double? {
        val df = DecimalFormat("#.##")
        df.roundingMode = RoundingMode.CEILING
        return df.format(number).toDouble()
    }

【讨论】:

    【解决方案2】:

    试试这个

    EditText amount = findViewById(R.id.amount);
    
    int amout =  Integer.parseInt(amount.getText().toString());
    
    int samount = amout * 180;
    
    String converted = new DecimalFormat("0.00").format(samount);
    

    【讨论】:

      猜你喜欢
      • 2021-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-22
      • 2017-03-22
      • 1970-01-01
      • 2014-08-18
      • 1970-01-01
      相关资源
      最近更新 更多