有没有一种方法可以将float转换为小数而不舍入并保留其精度? [英] Is there a way to cast float as a decimal without rounding and preserving its precision?
问题描述
看来,如果您
CAST(field1 as decimal) field1
这将自动添加舍入。
原始定义为:
field1 type:float length:8 prec:53
field1 type:float length:8 prec:53
我需要将其转换为十进制,因为我需要实体框架层才能将此字段生成为十进制(而不是双精度)。
I need to cast it to decimal, because I need my Entity Framework layer to generate this field as decimal (instead of double).
是否有一种将其转换为十进制的方法,以便保留原始精度,并且不舍入?
Is there a way to cast it as decimal, so that it preserves original precision, and doesn't round?
我想避免在转换中声明精度,因为:
I would like to avoid having to declare the precision in the cast, because:
- 有100多个涉及不同精度的字段,并且
- 如果基础表将来发生更改,则可能会导致无法预料的错误出现,并且
- 使管理更加困难。
推荐答案
您是否尝试过: / p>
Have you tried:
SELECT Cast( 2.555 as decimal(53,8))
这将返回 2.55500000
。
更新:
显然,您也可以使用SQL_VARIANT_PROPERTY查找值的精度和小数位数。示例:
Apparently you can also use SQL_VARIANT_PROPERTY to find the precision and scale of a value. Example:
SELECT SQL_VARIANT_PROPERTY(Cast( 2.555 as decimal(8,7)),'Precision'),
SQL_VARIANT_PROPERTY(Cast( 2.555 as decimal(8,7)),'Scale')
返回 8 | 7
您也许可以在转换过程中使用它...
You may be able to use this in your conversion process...
这篇关于有没有一种方法可以将float转换为小数而不舍入并保留其精度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!