1 Star 0 Fork 1

LiuYan/Java

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
文件
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
NumberFormatOptimization.java 1.15 KB
Copy Edit Raw Blame History
liuyan authored 2023-06-30 16:27 +08:00 . code refactor
import java.text.NumberFormat;
import java.util.Locale;
/**
* @author LiuYan
* @date 2023/5/15
*/
public class NumberFormatOptimization {
public static void main(String[] args) {
double number = 1234567.89;
// 使用默认本地化进行格式化
NumberFormat nfDefault = NumberFormat.getInstance();
String formatted = nfDefault.format(number);
System.out.println("Default Local Formatted Number:" + formatted); // 输出: 1,234,567.89
// 使用指定本地化进行格式化
Locale locale = new Locale("fr", "FR");
NumberFormat nfFrench = NumberFormat.getInstance(locale);
formatted = nfFrench.format(number);
System.out.println("Appoint Local Formatted Number:"+formatted); // 输出: 1 234 567,89 (注意空格)
// 解析本地化字符串中的数字
String numStr = "1,234,567.89";
try {
Number parsedNum = nfDefault.parse(numStr);
System.out.println("Parse Local Formatted Number:"+parsedNum.doubleValue()); // 输出: 1234567.89
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/Liu-Yan-Code/java.git
git@gitee.com:Liu-Yan-Code/java.git
Liu-Yan-Code
java
Java
master

Search