Java DecimalFormat数据格式化例子

        public static void main (String args[]) {
         
             DecimalFormat  dFormat = new DecimalFormat(".##");//2位小数,不够不补0,多余按四舍五入舍去
             String s= dFormat.format(333.339);
             System.out.println("s= "+s);
             
             dFormat.applyPattern(".00");//2位小数,不够补0
             s = dFormat.format(333.3);
             System.out.println("s1 = "+s);
             
             dFormat.applyPattern(".##\u2030");//按千分位格式,保留2位小数,增加千分号
             s = dFormat.format(0.1234567);
             System.out.println(s);
            
             dFormat.applyPattern("\u00A4###,###.00");//添加国家货币符号
             s = dFormat.format(34123456);
             System.out.println(s); 
             
             dFormat.applyPattern("$,###,###");
             System.out.println(dFormat.format(3333333));
             
             dFormat.applyPattern("0.0;-#.0");//正负数表示,保留1位小数
             s = dFormat.format(77.2229);
             System.out.println(s);
             s = dFormat.format(-77.111);
             System.out.println(s);
             
            
        } 

 

 

s= 333.34
s1 = 333.30
123.46‰
¥34,123,456.00
$3,333,333
77.2
-77.1

转载于:https://www.cnblogs.com/dot-dot-123/p/3662238.html

JAVA DecimalFormat 保留小数位以及四舍五入的陷阱 需求 业务需要导出的Excel的数字内容保留两位小数,并且四舍五入 代码实现 百度一圈所抄袭的代码 DecimalFormat dfScale2 = new DecimalFormat("###.##"); dfScale2.format(1.125D); 发现问题 导出数据很诡异.不是所有数据都是如所想的四舍五入. 经过排查最终发现是RoundingMode的问题,应该使用HALF_UP, D...... 阅读详情

相关推荐

Java数字格式化完全指南:DecimalFormat与String.format详解

本文对比分析了Java中两种主流的数字格式化方法:DecimalFormat和String.format()。DecimalFormat功能强大,通过模式字符串灵活控制数字格式,支持补零、千分位、百分比等多种格式化需求。String.format()则提供简洁的格式化方案,采用类似C语言的格式化符号,适合简单场景。文章通过代码示例详细展示了两种方法的基本用法、模式符号含义以及实际应用场景,为开发者选择适合的数字格式化方案提供了实用参考。

学无止境 6726

在 JavaScript 中给数字加上千位分隔符并保留二位小数

在前端业务中,我们经常需要对数字(比如说金额)进行处理,这个时候可能客户要求使用千分符(例如说:999,999,999 这样的数字)来对数字进行格式化,那么应该如何去做呢?

narukeu的博客 3263

告别VBA!用Python+PyWin32自动化你的SolidWorks建模流程(附完整代码)

本文详细介绍了如何利用Python和PyWin32实现SolidWorks建模流程的自动化,替代传统的VBA宏开发。通过环境配置、宏代码转换、高级开发模式及工程化实践等实战内容,帮助机械设计师构建高效、可维护的自动化工作流,显著提升设计效率。

weixin_30485379的博客 371

价格格式化--小数保留两位,整数部分用千分号分割

在涉及到价格展示的时候,一般 有千分号分割和保留两位小数的需求 //保留两位小数以及千分位的分隔符 function toDoubleThousands(num) { if (!num) { return "0.00"; } //判断是否有小数点 var s = num.t...

weixin_30500473的博客 371

Zabbix7.0+DeepSeek大模型实现人工智能告警分析

本方案基于Zabbix7.0监控系统,通过底层webhook脚本机制集成Deepseek做故障分析提供解决方案,构建智能化运维体系。

xingdiango的博客 2383

建行报专版报道建设银行金融科技发展历程

经过十多年的建设,总行前中后台部门都有了自己的业务系统,但系统“部门”特征显著,渠道、客户、产品的横向整合不足,产品系统层次化、结构化、参数化程度不高,渠道支持产品快速部署以及跨渠道服务能力不足,管理分析类系统建设相对滞后,系统较弱的可扩展能力在应对高速增长的交易量时已疲态尽显,同时安全稳定运行的压力和风险日益积聚。站在新的历史起点上,科技条线将聚焦主责主业,完善信息科技管理体系,紧紧围绕安全生产中心任务,持续推动技术能力复用共享和自主可控,为全行做好金融五篇大文章,推动业务高质量发展提供技术支撑。

银行信息系统应用架构详解 396

js千分位符和数字的相互转换并保留两位小数

【代码】js千分位符和数字的相互转换并保留两位小数。

tanghuan0827的博客 1801

主流反爬虫、反作弊防护与风控对抗手段

主流反爬虫、反作弊风控防护中的核心设计点与对抗策略

吴秋霖的博客 2565

js parsefloat

项目中需要对返回的小数进行格式化,把零省略掉。 1.00   ---> 1 1.01   ---> 1.01 1.10   ---> 1.1   parseFloat() 函数可解析一个字符串,并返回一个浮点数。 该函数指定字符串中的首个字符是否是数字。如果是,则对字符串进行解析,直到到达数字的末端为止,然后以数字返回该数字,而不是作为字符串。 语法 parseFloat(str...

weixin_33816946的博客 316

Cadence OrCAD Capture 保持元器件编号情况复制元件,复制后元器件编号与复制前不同的问题解决

如下图所示复制时,已经勾选了“Preserve reference on copy”但是复制后的编号却与复制前不一样。本文简单介绍其原因及解决方法。

fydar的博客 3173

ThingsBoard集群部署之k8s

今天终于有时间去搞这个啦,拖了很久了,一直没时间,因为我本地没有那么多机器资源,开虚拟机不够,如果租用阿里云服务器,需要有充值的时间,因为这个费用是按小时付费,需要有连贯的时间来搞才行,今天恰好有时间,就开始搞了,弄成功搞出来了,特地写博客记录下来。

AldrichEugene 1177

数字格式化返回字符串

public static DecimalFormat seqFormat = new DecimalFormat("000.000"); //按照指定的 字符形式格式化 返回字符串 public static void main(String[] args) {    String  str =  seqFormat.format(13332.34532);  System.out.printl...

除了你无可取代 197

基于STM32L4XX、HAL库的DP83822 以太网收发器(PHY)驱动程序设计

一、简介:DP83822是德州仪器(TI)推出的一款低功耗、高性能10/100Mbps以太网物理层收发器(PHY)。它具有以下主要特性:支持10BASE-T和100BASE-TX以太网标准低功耗设计,适合嵌入式应用MII/RMII接口,方便与微控制器连接内置自检和诊断功能支持自动协商和手动配置工业级温度范围(-40°C至+85°C)

zhusl6688的博客 549

Java Dev Notes - the Chaos

LinkedList 双向链表ListArray 数组列表

210

【推荐100个unity插件】动画重定向,快速在不同角色间共享动画,共用非人形Humanoid(泛型generic)动画——Retarget Pro

Retarget Pro是一款高效的Unity动画重定向工具,可快速在不同角色间共享动画。主要特点包括:1分钟快速设置,支持编辑器和运行时重定向;适用于人形、动物、武器等各类角色;提供基础重定向、IK重定向和FPS专用重定向三种功能模块;通过Rig组件和骨架资产实现骨骼链匹配,支持批量处理。插件包含示例项目和预设资源,操作简便且效果优于Unity原生Humanoid系统,特别适合处理手指等精细动画。

向宇的博客,专注php/web全栈 unity游戏开发,欢迎大家评论纠错 564

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

java格式化数字 NumberFormat及DecimalFormat

NumberFormat帮助您格式化和解析任何区域设置的数字。您的代码可以完全独立于小数点,千位分隔符的区域设置约定,甚至是使用的特定十进制数字,或者数字格式是否为十进制。

初夏0811的博客 3万+

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

java格式化double类型的数据:new DecimalFormat("pattern")

java中采用DecimalFormat类可以对数字进行格式化,用法如下: 注:下图中pattern字符串中的数字0也可以用#代替: 用0的时候:表示数字没有达到指定的位数时,用0补齐; 用#的时候:表示数字没有达到指定位数时,就不用补齐。 示例: DecimalFormat df = new DecimalFormat("###.0"); System.out.println(df....

统木木的博客 8397

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

DecimalFormat - 格式化数据

开发过程中,通常会遇到一个问题:我们需要将一个数值转换为格式化的数值,比:3.145678保留两位有效数字,这时候我们该用什么方法来转换呢? 这篇笔记,将教会我们如何将String、double、float格式化为我们所需要的格式(比如:保留两位小数),而我们要用到的对象就是:DecimalFormat。 1、DecimalFormat的介绍 全路径名:java.text.DecimalFormat JavaSE官方文档的描述: DecimalFormat 是 NumberFormat 的一个具.

菜菜的大数据开发の路 2004

数据格式化(DecimalFormat)

数据格式化 一、DecimalFormat类 此类在java.text包下; 方法: DecimalFormat(String pattern):有参构造,实例化时指定格式; DecimalFormat():无参构造; applyPattern(String pattern):设置格式; format(value):将数据格式化; setGroupingSize(int size):设置将数字分组的大小 setGroupingUsed(boolean true):设置是否支持分组 pattern: ①、0

studying的博客 3133

java DecimalFormat 数据四舍五入格式化处理

java中数据四舍五入格式化处理 最近的开发中,被测试的小伙伴告知,相同四舍五入处理逻辑的数据有一个有问题。当时的自己是懵逼的,我的处理方式是这样的,业务要求是四舍五入保留两位小数: DecimalFormat d = new DecimalFormat("#0.00"); d.format(0.145d); 这样的数据,期望结果是0.15,但是实际的结果是0.14,,,,,为什么呢?为什么呢? ...

SYP091319920901的博客 1176

java数字格式化-DecimalFormat的用法

DecimalFormat 是 NumberFormat 的一个具体子类,用于格式化十进制数字。 DecimalFormat 包含一个模式 和一组符号 符号含义: 0 一个数字 一个数字,不包括 0 . 小数的分隔符的占位符 , 分组分隔符的占位符 ; 分隔格式。 缺省负数前缀。 % 乘以 100 和作为百分比显示 ? 乘以 1000 和作为千进制货币符显示;用货币符号代替;如果双写,用 国际货币符号代替。如果出现在一个模式中,用货币十进制分隔符代 替十进制分隔符。 X 前缀或后缀中使用的任何其它字符,

qq_43211295的博客 894

关于Java中DecimalFormat()方法的调用

关于Java中DecimalFormat()方法的调用 我们可以从各种地方知道DecimalFormat类的使用方法,它的作用是格式化数字。 在此不多赘述,博主现在是一个本科生也处于初学Java阶段,这里就讲一下关于DecimalFormat类的构造方法的发现与探索。 我分别建立了两个方法,一个用有参方法实例化对象,另一个用无参方法实例化对象。 public class Decimal...

2万+

Java 17 四种 double 格式化方案对比:BigDecimal vs DecimalFormat vs String.format vs NumberFormat

本文深度评测Java 17中四种double格式化方案(BigDecimal、DecimalFormat、String.format和NumberFormat),重点分析它们在保留两位小数时的精度控制、性能表现及多线程安全性。通过实测数据和场景化选型矩阵,为金融计算、日志输出等场景提供最佳实践指南,帮助开发者优化数值处理效率与准确性。

weixin_30240349的博客 519

使用 DecimalFormat 和 NumberFormat 处理数字的格式化显示问题(千分位、百分号等)

前言 原来没有接触过DecimalFormat,没有想到还有这玩意。使用的时候还是不错的,详细的了解可以看官方的api。找到了中文的api,小伙子们可以直接跳转啊! DecimalFormat 是 NumberFormat 的一个具体子类,用于格式化十进制数字。 DecimalFormat 包含一个模式 和一组符号。差点忘记说它是干嘛的。 符号含义: 上一个经典的表格,我们来说一说响应的符号的意义: 符号 位置 本地化? 含义 0 数字 是 阿拉伯数字 # 数字 是 阿拉伯数字如果不存在就

tc979907461的博客 9916

java 数字格式化:小数点、百分比 NumberFormat Decimalformat

java.text 包中的一些包可以处理这类问题 import java.text.NumberFormat; 1。Decimalformat df1 = new Decimalformat("####.000");          System.out.println(df1.format(1234.56));      显示:1234.560 2。NumberFormat n

tanyit的专栏 4万+
上一篇: QTQBitmap 与QPixmap之间的区别
下一篇: myisam和innodb的区别
weixin_30270889
博客等级 码龄11年 109粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值