第二百零九节 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 Boot 2.7.8 集成 Thymeleaf的最佳实践与常见问题讨论

《SpringBoot2.7.8集成Thymeleaf的最佳实践与常见问题讨论》本文详细介绍了如何将SpringBoot2.7.8与Thymeleaf集成,从项目依赖到配置文件设置,再到控制器... 目录前言一、如何构建SpringBoot应用1、项目依赖 (pom.XML)2、控制器类3、Thymelea

SpringBoot项目jar依赖问题报错解析

《SpringBoot项目jar依赖问题报错解析》本文主要介绍了SpringBoot项目中常见的依赖错误类型、报错内容及解决方法,依赖冲突包括类找不到、方法找不到、类型转换异常等,本文给大家介绍的非常... 目录常见依赖错误类型及报错内容1. 依赖冲突类错误(1) ClassNotFoundExceptio

springboot控制bean的创建顺序

《springboot控制bean的创建顺序》本文主要介绍了spring-boot控制bean的创建顺序,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随... 目录1、order注解(不一定有效)2、dependsOn注解(有效)3、提前将bean注册为Bea

Java中的ConcurrentBitSet使用小结

《Java中的ConcurrentBitSet使用小结》本文主要介绍了Java中的ConcurrentBitSet使用小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、核心澄清:Java标准库无内置ConcurrentBitSet二、推荐方案:Eclipse

java中的Supplier接口解析

《java中的Supplier接口解析》Java8引入的Supplier接口是一个无参数函数式接口,通过get()方法延迟计算结果,它适用于按需生成场景,下面就来介绍一下如何使用,感兴趣的可以了解一下... 目录1. 接口定义与核心方法2. 典型使用场景场景1:延迟初始化(Lazy Initializati

Java中ScopeValue的使用小结

《Java中ScopeValue的使用小结》Java21引入的ScopedValue是一种作用域内共享不可变数据的预览API,本文就来详细介绍一下Java中ScopeValue的使用小结,感兴趣的可以... 目录一、Java ScopedValue(作用域值)详解1. 定义与背景2. 核心特性3. 使用方法

spring中Interceptor的使用小结

《spring中Interceptor的使用小结》SpringInterceptor是SpringMVC提供的一种机制,用于在请求处理的不同阶段插入自定义逻辑,通过实现HandlerIntercept... 目录一、Interceptor 的核心概念二、Interceptor 的创建与配置三、拦截器的执行顺

Java中Map的五种遍历方式实现与对比

《Java中Map的五种遍历方式实现与对比》其实Map遍历藏着多种玩法,有的优雅简洁,有的性能拉满,今天咱们盘一盘这些进阶偏基础的遍历方式,告别重复又臃肿的代码,感兴趣的小伙伴可以了解下... 目录一、先搞懂:Map遍历的核心目标二、几种遍历方式的对比1. 传统EntrySet遍历(最通用)2. Lambd

Spring Boot 中 RestTemplate 的核心用法指南

《SpringBoot中RestTemplate的核心用法指南》本文详细介绍了RestTemplate的使用,包括基础用法、进阶配置技巧、实战案例以及最佳实践建议,通过一个腾讯地图路线规划的案... 目录一、环境准备二、基础用法全解析1. GET 请求的三种姿势2. POST 请求深度实践三、进阶配置技巧1

springboot+redis实现订单过期(超时取消)功能的方法详解

《springboot+redis实现订单过期(超时取消)功能的方法详解》在SpringBoot中使用Redis实现订单过期(超时取消)功能,有多种成熟方案,本文为大家整理了几个详细方法,文中的示例代... 目录一、Redis键过期回调方案(推荐)1. 配置Redis监听器2. 监听键过期事件3. Redi