gpt4 book ai didi

java 使用 char 格式化字符串到小数点后两位

转载 作者:太空宇宙 更新时间:2023-11-04 06:20:24 26 4
gpt4 key购买 nike

所以我在格式化字符串时遇到问题。

我的输出应该始终是 ##.##C or F (温度换算)

这是我的方法:

public void setResultLabel(double inNumber, char inChar)
{

String number, letter;

number = String.valueOf(inNumber);

letter = String.valueOf(inChar);

String temp;

temp = number + letter;

String format = String.format("%2.5s", temp); /* how do you make this code
so that the out put result will always be up to two decimal places
with a char C or F? */

result.setText(format);

add(result);
}

最佳答案

public void setResultLabel(double inNumber, char inChar)
{

String number, letter;
DecimalFormat decimalFormat = new DecimalFormat("##.##"+inChar);


String formated = decimalFormat.format(inNumber);
/* formated is your result */
// System.out.println(formated);

result.setText(format);
add(result);
}

关于java 使用 char 格式化字符串到小数点后两位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27466389/

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