在Java和Kotlin中,有一个API可以用来显示时间而不必创建字符串资源。
在下面的示例代码行中,此值允许午夜后8个小时的时间根据设备区域设置自动更改其显示方式。
val timeCustom = LocalTime.of(8, 0)
是否有类似的东西可用于十进制数字,其中该值会自动使用在区域设置上附加的特定符号? (
.
或,
)。例如,描述某物的高度(例如5点2米):
val decimalNumber = Decimal.of(5,2)
有这样的东西吗?
最佳答案
您可以使用Locale.getDefault()
例如
import java.text.NumberFormat;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
Locale currentLocale = Locale.getDefault();
Integer quantity = 123456;
Double amount = 345987.246;
NumberFormat numberFormatter;
String quantityOut;
String amountOut;
numberFormatter = NumberFormat.getNumberInstance(currentLocale);
quantityOut = numberFormatter.format(quantity);
amountOut = numberFormatter.format(amount);
System.out.println(quantityOut);
System.out.println(amountOut);
}
}
输出:
123,456
345,987.246
关于java - 如何在不创建字符串资源的情况下使用十进制数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61460862/