保留Double值的格式 - Java [英] Retain formatting of a Double value - Java

查看:106
本文介绍了保留Double值的格式 - Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Poi在Java中创建Excel工作簿。我的原始数据以字符串形式出现。我需要格式化数据,将两个小数位输入到正在写入数字的单元格中。我使用 Double.parseDouble()将字符串转换为数字,然后使用 DecimalFormat 将数字格式化为字符串。另一个调用 Double.parseDouble()将值返回到numeric(它所在的单元格格式为数字,所以我不能使用字符串值),我应该很好。问题是,第二次调用 Double.parseDouble()会截断小数点右边的任何尾随零。任何人都知道我怎么能强迫这个值读取为1.50而不是1.5?

I am using Poi to create Excel workbooks in Java. My raw data comes in as a string. I need to format the data to enter two decimal places into the cell where the number is being written. I use Double.parseDouble() to convert the string to numeric and then use DecimalFormat to format the numeric as a string. Another call to Double.parseDouble() to return the value to numeric (the cell where it is going is formatted numeric, so I can't use the string value) and I should be good. Problem is, that second call to Double.parseDouble() truncates any trailing zeroes off from the right of the decimal point. Anybody have an idea as to how I can coerce this value to read as, say, 1.50 rather than 1.5?

推荐答案

它出现了我们陷入了僵局。正如Mario所指出的,双打被管理为二进制,并且没有办法将二进制格式化为双,除了将它转换为带有DecimalFormat的字符串,它不再是double。我向老板解释了这一点,并且他对采取原始双重的解决方案没问题,所以我正在关闭这个问题。感谢所有人的帮助和支持。

It appears we are at an impasse. As Mario pointed out, doubles are managed as binary and there is no way to format the binary as a double, except to convert it to a string with DecimalFormat, which is no longer a double. I explained this to my boss and he's ok with the solution of taking the raw double, so I'm closing this issue. Thanks to all for your help and support.

问候,
Mike

regards, Mike

这篇关于保留Double值的格式 - Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆