第二百零九节 Java格式 - Java数字格式类

2024-08-23 01:44

本文主要是介绍第二百零九节 Java格式 - Java数字格式类,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Java格式 - Java数字格式类

以下两个类可用于格式化和解析数字:

  • java.text.NumberFormat
  • java.text.DecimalFormat

NumberFormat 类可以格式化一个数字特定地区的预定义格式。

DecimalFormat 类可以格式化数字以特定区域设置的自定义格式。

NumberFormat类的 getXXXInstance()方法返回格式化程序对象的实例。

XXX可以由数字,货币,整数或百分比替换,或只是getInstance()。这些方法都是重载的。

如果你调用它们没有参数,它们返回一个格式化对象默认语言环境。

使用number参数调用format()方法以将格式化的数字作为字符串。

import java.text.NumberFormat;public class Main {public static void main(String[] args) {NumberFormat formatter;// Get number formatter for default localeformatter = NumberFormat.getInstance();System.out.println(formatter.format(12312.123123));}
}

上面的代码生成以下结果。

例子

下面的代码说明了如何以默认格式为当前语言环境,法语语言环境和德语语言环境格式化数字。

import java.text.NumberFormat;
import java.util.Locale;public class Main {public static void main(String[] args) {double value = 123456789.9876543;// Default localeprintFormatted(Locale.getDefault(), value);// Indian localeLocale indianLocale = new Locale("en", "IN");printFormatted(indianLocale, value);}public static void printFormatted(Locale locale, double value) {// Get number and currency formatterNumberFormat nf = NumberFormat.getInstance(locale);NumberFormat cf = NumberFormat.getCurrencyInstance(locale);System.out.println("Format value: " + value + "  for locale: " + locale);System.out.println("Number: " + nf.format(value));System.out.println("Currency: " + cf.format(value));}
}

上面的代码生成以下结果。

DecimalFormat类

要执行更高级的格式化,我们可以使用DecimalFormat类。

DecimalFormat类允许我们提供我们自己的格式模式。 的下表显示模式及其用法。

符号位置含义
0Number代表数字
#Number数字,零显示为不存在
.Number小数分隔符或货币小数分隔符
-Number减号
,Number分组分隔符
ENumber以科学记数法分隔尾数和指数。
;子模式边界分隔正和负子模式
%字首或字尾乘以100并以百分比显示
\u2030字首或字尾乘以1000,并显示为每毫米值

一旦我们创建了DecimalFormat类的对象,就可以改变格式模式使用其 applyPattern()方法。

import java.text.DecimalFormat;public class Main {private static DecimalFormat formatter = new DecimalFormat();public static void main(String[] args) {formatNumber("##.##", 12.345);formatNumber("##.##", 12.345);formatNumber("0000.0000", 12.345);formatNumber("#.##", -12.345);// Positive and negative number format formatNumber("#.##;(#.##)", -12.735);}public static void formatNumber(String pattern, double value) {// Apply the pattern formatter.applyPattern ( pattern );String formattedNumber = formatter.format(value);System.out.println("Number:" + value + ", Pattern:" + pattern+ ", Formatted Number:" + formattedNumber);}
}

上面的代码生成以下结果。

解析

我们还可以使用 parse()方法将字符串解析为数字。 parse()方法返回 java.lang.Number 类的对象。

我们可以使用 java.lang.Number 类中的xxxValue()方法来获取原始值,其中xxx可以是byte,double,float,int,long和short。

import java.text.DecimalFormat;
import java.text.ParsePosition;public class Main {private static DecimalFormat formatter = new DecimalFormat();public static void main(String[] args) {// Parse a string to decimal numberString str = "qq1,234.567";String pattern = "#,###.###";formatter.applyPattern(pattern);// Create a ParsePosition object to specify the first digit of// number in the string. It is 1 in "qq1,234.567"// with the index 2.ParsePosition pp = new ParsePosition(2);Number numberObject = formatter.parse(str, pp);double value = numberObject.doubleValue();System.out.println("Parsed Value  is " + value);}}

上面的代码生成以下结果。

以上

这篇关于第二百零九节 Java格式 - Java数字格式类的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1097996

相关文章

Spring Cloud Hystrix原理与注意事项小结

《SpringCloudHystrix原理与注意事项小结》本文介绍了Hystrix的基本概念、工作原理以及其在实际开发中的应用方式,通过对Hystrix的深入学习,开发者可以在分布式系统中实现精细... 目录一、Spring Cloud Hystrix概述和设计目标(一)Spring Cloud Hystr

Spring Boot整合消息队列RabbitMQ的实现示例

《SpringBoot整合消息队列RabbitMQ的实现示例》本文主要介绍了SpringBoot整合消息队列RabbitMQ的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的... 目录RabbitMQ 简介与安装1. RabbitMQ 简介2. RabbitMQ 安装Spring

springMVC返回Http响应的实现

《springMVC返回Http响应的实现》本文主要介绍了在SpringBoot中使用@Controller、@ResponseBody和@RestController注解进行HTTP响应返回的方法,... 目录一、返回页面二、@Controller和@ResponseBody与RestController

JAVA集成本地部署的DeepSeek的图文教程

《JAVA集成本地部署的DeepSeek的图文教程》本文主要介绍了JAVA集成本地部署的DeepSeek的图文教程,包含配置环境变量及下载DeepSeek-R1模型并启动,具有一定的参考价值,感兴趣的... 目录一、下载部署DeepSeek1.下载ollama2.下载DeepSeek-R1模型并启动 二、J

springboot rocketmq配置生产者和消息者的步骤

《springbootrocketmq配置生产者和消息者的步骤》本文介绍了如何在SpringBoot中集成RocketMQ,包括添加依赖、配置application.yml、创建生产者和消费者,并展... 目录1. 添加依赖2. 配置application.yml3. 创建生产者4. 创建消费者5. 使用在

Spring Retry 实现乐观锁重试实践记录

《SpringRetry实现乐观锁重试实践记录》本文介绍了在秒杀商品SKU表中使用乐观锁和MybatisPlus配置乐观锁的方法,并分析了测试环境和生产环境的隔离级别对乐观锁的影响,通过简单验证,... 目录一、场景分析 二、简单验证 2.1、可重复读 2.2、读已提交 三、最佳实践 3.1、配置重试模板

Spring中@Lazy注解的使用技巧与实例解析

《Spring中@Lazy注解的使用技巧与实例解析》@Lazy注解在Spring框架中用于延迟Bean的初始化,优化应用启动性能,它不仅适用于@Bean和@Component,还可以用于注入点,通过将... 目录一、@Lazy注解的作用(一)延迟Bean的初始化(二)与@Autowired结合使用二、实例解

SpringBoot使用Jasypt对YML文件配置内容加密的方法(数据库密码加密)

《SpringBoot使用Jasypt对YML文件配置内容加密的方法(数据库密码加密)》本文介绍了如何在SpringBoot项目中使用Jasypt对application.yml文件中的敏感信息(如数... 目录SpringBoot使用Jasypt对YML文件配置内容进行加密(例:数据库密码加密)前言一、J

Java中有什么工具可以进行代码反编译详解

《Java中有什么工具可以进行代码反编译详解》:本文主要介绍Java中有什么工具可以进行代码反编译的相关资,料,包括JD-GUI、CFR、Procyon、Fernflower、Javap、Byte... 目录1.JD-GUI2.CFR3.Procyon Decompiler4.Fernflower5.Jav

Spring Boot 中正确地在异步线程中使用 HttpServletRequest的方法

《SpringBoot中正确地在异步线程中使用HttpServletRequest的方法》文章讨论了在SpringBoot中如何在异步线程中正确使用HttpServletRequest的问题,... 目录前言一、问题的来源:为什么异步线程中无法访问 HttpServletRequest?1. 请求上下文与线