简体   繁体   English

Oracle是否为Number数据类型存储尾随零?

[英]Does Oracle store trailing zeroes for Number data type?

When i am storing numeric values in a table and querying that table in SQL Developer, it is not showing trailing zeroes for Number data type. 当我在表中存储数值并在SQL Developer中查询该表时,它没有显示Number数据类型的尾随零。

create table decimal_test(decimal_field number(*,10));

insert into decimal_test(decimal_field) values(10);
insert into decimal_test(decimal_field) values(10.11);
insert into decimal_test(decimal_field) values(10.100);
insert into decimal_test(decimal_field) values(10.00);

select * from decimal_test;

and the results are 结果是

10
10.11
10.1
10

These values are processed from java code. 这些值是从java代码处理的。 In this case i am using BigDecimal to store the value. 在这种情况下,我使用BigDecimal来存储值。

Before saving to DB, if i have BigDecimal(10.00) , after saving, the value returned from DB is BigDecimal(10) . 在保存到DB之前,如果我有BigDecimal(10.00) ,保存后,从DB返回的值是BigDecimal(10) Equals method in BigDecimal fails because the scale is changed. BigDecimal Equals方法失败,因为比例已更改。

And i the decimal precision is not constant. 而且我的小数精度不是恒定的。 User can set BigDecimal(10.00) or BigDecimal (10.000) etc. Because of this, value needs to stored in DB as it is. 用户可以设置BigDecimal(10.00)或BigDecimal (10.000)等。因此,值需要按原样存储在DB中。

Is there any way to store the trailing zeros in oracle? 有没有办法在oracle中存储尾随零?

The existence of the trailing zeros is a display issue, not a storage issue. 尾随零的存在是显示问题,而不是存储问题。 The trailing zeros are not significant, and anyway the internal format of the numbers is immaterial as long as the values are correct. 尾随零并不重要,无论如何,只要正确,数字的内部格式就不重要了。 There is no value difference between 10 and 10.00000 . 1010.00000之间没有价值差异。

If you need trailing zeros you can always use formatting when converting the values for display. 如果需要尾随零,则在转换显示值时始终可以使用格式。 For example: 例如:

System.out.printf("%10.4d\n", decimalValue);

If the problem is differences in scale, you can set the scale to the appropriate value before comparing. 如果问题是在规模上的差异,可以在比较之前设定的规模,以适当的值。

If you want to preserve the scale of the numeric input (which can be significant - a measure of 10.1 is different from a measure 10.100) store the scale in an extra column. 如果要保留数字输入的比例(这可能很重要 - 10.1的度量与度量10.100不同)将比例存储在额外列中。

create table decimal_test
(decimal_field number(*,10),
 decimal_scale number(*,0)
);

Calculate the scale of the input and store it in the new column decimal_scale . 计算输入的比例并将其存储在新列decimal_scale

Use this value in the output formating. 在输出格式化中使用此值。

I'd not recommend to store the number as a string - you preserve the scale but loose the number validation and potentially confuse the CBO with wrong estimation caused by the type conversion... 不建议将数字存储为字符串 - 保留比例但是数字验证松散,可能会使CBO混淆由类型转换引起的错误估计...

10 and 10.00 are generally the same numbers. 10和10.00 通常是相同的数字。 (It doesn't make a difference if you have to pay 10 $ or 10.00 $ or even 10.0000 $ :-) (如果您需要支付10美元或10.00美元甚至10.0000美元,这没有任何区别:-)

If, however, you want to express the precision of the value as in measurements, so 10.00 means you know the number only up to two decimal places, but the real number can be 10.00001 or 10.009 or whatever, then you must additonally store this information. 但是,如果您想要像测量中那样表示值的精度,那么10.00表示您只知道最多两位小数的数字,但实数可以是10.00001或10.009或者其他什么,那么您必须另外存储此信息。 One way to do this would be using a string datatype, but then you'd have to stick to a certain format ('10.00' or say '10,00') and shouldn't use this for calculations in the database. 一种方法是使用字符串数据类型,但是你必须坚持某种格式('10 .00'或者说'10,00'),不应该使用它来进行数据库中的计算。

Another option would be store the number and an additional column for the precision of decimal places, eg value = 10 / decimal_places = 2. I would prefer this over storing a string. 另一种选择是存储数字和附加列的小数位数精度,例如value = 10 / decimal_places = 2.我宁愿这样存储字符串。

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

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