简体   繁体   English

DecimalFormat:没有前导零,没有指数,变量精度

[英]DecimalFormat: No leading zero, no exponential, variable precision

I'm using an Oracle DB together with JDBC. 我正在使用Oracle DB和JDBC。 I have to select some values and write them to a file. 我必须选择一些值并将它们写入文件。

First I used ResultSet#getString which gave me almost all values formatted as desired except sometimes numbers were represented with an exponential. 首先我使用了ResultSet#getString ,它给了我几乎所有按照需要格式化的值,有时数字用指数表示。 Eg: 0.0897234E-4 例如: 0.0897234E-4

In order to get rid of the exponential I checked if the type of the current column is NUMERIC and then used ResultSet#getBigDecimal to get a BigDecimal. 为了摆脱指数,我检查了当前列的类型是否为NUMERIC,然后使用ResultSet#getBigDecimal来获取BigDecimal。 I've done that because there are all kinds of Java number types stored in the DB and as Oracle DB only provides NUMERIC as type for numbers, I have to use the BigDecimal because every numeric Java type can be stored in a BigDecimal. 我之所以这样做是因为数据库中存储了各种Java数字类型,而且Oracle DB只提供NUMERIC作为数字的类型,我必须使用BigDecimal,因为每个数字Java类型都可以存储在BigDecimal中。
Then I used the BigDecimal#toPlainString method to effectively get rid of the exponential. 然后我使用BigDecimal#toPlainString方法有效地摆脱了指数。

if (rset.getMetaData().getColumnType(i) == java.sql.Types.NUMERIC) {
    value = (rset.getBigDecimal(i) != null rset.getBigDecimal(i).toPlainString() : "0");
}

The next problem was, that I then got a leading zero when the number was below 1 but I don't want them. 接下来的问题是,当数字低于1时,我得到一个前导零,但我不想要它们。

  • Before .007346 <-- desired 之前.007346 < - 期望
  • After 0.007346 0.007346之后
  • Before -.4352 <-- desired -.4352之前< - 期望
  • After -0.4352 -0.4352之后

To deal with that problem I searched the internet and found the DecimalFormat class. 为了解决这个问题,我搜索了互联网并找到了DecimalFormat类。 I was then able to format the numbers so that I don't have leading zeros with the following format: 然后我能够格式化数字,以便我没有以下格式的前导零:

new DecimalFormat("#.");

But this of course didn't display any digits after the decimal point but does add a decimal point after every number. 但这当然没有在小数点后显示任何数字,但在每个数字后面都会添加一个小数点。 Eg: 1235. -65. 例如: 1235. -65.

So what I want is, that if the number is decimal there should be as much digits as needed (actually 38, I think is in Oracle DB the max). 所以我想要的是,如果数字是十进制的,那么应该有所需的数字(实际上是38,我认为在Oracle DB中是最大的)。 There should never be exponentials and if the number is below 1 there shouldn't be a leading zero. 永远不应该有指数,如果数字低于1,则不应该有前导零。 If the number is natural there shouldn't be a decimal point at the end. 如果数字是自然的,那么最后不应该有一个小数点。

Some examples of the desired format: 9873478 -1349 .743803 -.004726 所需格式的一些示例: 9873478 -1349 .743803 -.004726

How can I achieve such a representation? 我怎样才能实现这样的表现? Any solution is welcome I don't have to use the DecimalFormat. 欢迎任何解决方案我不必使用DecimalFormat。 Could the solution possibly be, that I have to determine the type after getting the number as a BigDecimal by trying to convert it to the different Java types? 可能的解决方案是,我必须通过尝试将数字转换为不同的Java类型来获取数字作为BigDecimal之后确定类型吗?

What about new DecimalFormat(".#"); new DecimalFormat(".#");怎么样new DecimalFormat(".#"); ?

You were very close to the answer, indeed. 确实,你非常接近答案。 As you can see the behaviour of the decimals in your attempt is the behaviour you expected on the integer part. 正如您所看到的,尝试中小数的行为是您在整数部分上所期望的行为。

As you can read in Javadoc # shows zero as absent 正如你可以在Javadoc中看到的那样#显示为零缺席

EDIT 编辑

As stated in the comments, the problem with the solution above is that you get only one decimal. 如评论中所述,上述解决方案的问题是您只得到一个小数。 You'd maybe better define your layout by the API rather than with a pattern. 您可以通过API而不是模式更好地定义布局。

DecimalFormat format = new DecimalFormat();
format.setMinimumIntegerDigits(0);
format.setMaximumFractionDigits(2000);//should be more than enough

Note that you could also improve the first solution 请注意,您还可以改进第一个解决方案

DecimalFormat format = new DecimalFormat(".##################################################################");

...but it is less easy to define a large number of fraction digits (if you really need it). ...但是定义大量小数位(如果你真的需要它)则不那么容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM