gpt4 book ai didi

java - 如何只显示小数点后两位数

转载 作者:太空宇宙 更新时间:2023-11-03 12:58:52 24 4
gpt4 key购买 nike

只想显示小数点后的两位数,我试过了,但我仍然在小数点后得到很多位:

itemamount = Double.parseDouble(text_cost_code.getText().toString());
txt_total.setText(new DecimalFormat("##.##").format(itemamount));

edit_qty_code.addTextChangedListener(new TextWatcher() {

public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
if (!edit_qty_code.getText().toString().equals("")
|| !edit_qty_code.getText().toString().equals("")) {
itemquantity = Double.parseDouble(edit_qty_code.getText().toString());
itemamount = Double.parseDouble(text_cost_code.getText().toString());
txt_total.setText(new DecimalFormat("##.##").format (itemquantity * itemamount));
} else { txt_total.setText("0.00");}}

最佳答案

我不确定您要在代码中的哪个位置执行此操作,但是

String.format("%.2f", value);

应该可以。

关于java - 如何只显示小数点后两位数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14659072/

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