铸造为小数和舍入 [英] casting as decimal and rounding

查看:167
本文介绍了铸造为小数和舍入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以看起来如果你

CAST(field1 as decimal) field1

这将自动添加四舍五入。

this will automatically add rounding.

原始定义为:

field1 type:float length:8 prec:53

field1 type:float length:8 prec:53

我需要把它转换为decimal,因为我需要我的Entity Framework层生成这个字段为decimal(而不是double)

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?

我想避免必须声明转换的精度,因为1.有100个字段涉及不同的精度,并且1.如果基础表更改

i would like to avoid having to declare the precision in the cast, because 1. there are 100's of fields involved with varying precision, and 1. if the underlying table changes in the future, it could cause unforeseen bugs to emerge, and 3. makes the management more difficult

推荐答案

您尝试过:

SELECT Cast( 2.555 as decimal(53,8))

这将返回 2.55500000 。这是你想要的吗?

This would return 2.55500000. Is that what you want?

UPDATE:

显然你也可以使用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...

这篇关于铸造为小数和舍入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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