调整小数点 [英] Adjusting the decimal point

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

问题描述

在我的项目中,找到百分比的同时用大量数字表示.我想将小数点调整2个位置.例如:12.36987为12.37,而不是12.37000.我该怎么办?

In my project, while finding the percentage it is represented by a large number of digits. I wants to adjust the decimal point by 2 positions. Eg: 12.36987 as 12.37, not as 12.37000. How can I do it?

推荐答案

不是那么简单:如果要将值作为浮点数返回给另一个应用程序,则没有确保您舍入的数字在浮点数中可以表示为2个十进制数字.如果不是,那么它将再次以长整数形式返回,只是这次的精度不如它开始时那样.

共有三个选项:
1)返回原始数字,然后让软件的表示层确定其要显示的位数(推荐)

2)将数字转换为数字,并希望获得最好的结果:
It''s not that simple: If you are returning the value to another application as a floating point number, then there is no guarantee that the number you have rounded to is representable in floating point as a 2 decimal digit number. If it isn''t then it will be returned as a long number again, only this time less precisely than it started as.

There are three options:
1) Return the original number and let the presentation layer of your software decide how many digits it wants to display (recommended)
or
2) Return the number cast to a numeric and hope for the best:
SELECT CONVERT(decimal(20,2),myNumber) FROM myTable



3)将数字作为字符串而不是数字值返回.


or
3) Return the number as a string instead of a numeric value.

SELECT CONVERT(varchar, CONVERT(decimal(20,2),myNumber)) FROM myTable


声明保存该值的变量..

declare the variable which holds the value as this..

DECLARE @result numeric(18,2)



或如果值在表中,则将列数据类型更改为相同的数值(18,2)



or if the value is in the table, then change the column datatype to the same numeric(18,2)


Hi Zukiari,
打开SSMS,然后转到表的design view,然后选择decimal field行.
column properties视图中,将其scale 设置为 2 .
否则,如果您不想更改数据库设计,那么OriginalGriff提出的建议是合适的.

希望对您有帮助.
Hi Zukiari,
Open SSMS then goto design view of your table then select your decimal field row .
In column properties view set its scale to 2 .
Otherwise if you don''t want to change your DB design then what OriginalGriff proposed is suitable.

Hope It Helps.


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

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