Tho*_*mas 110
假设您的百分比有两位小数,您使用的数据类型取决于您计划存储百分比的方式.如果你要存储它们的小数等价物(例如100.00%存储为1.0000),我会将数据存储在一个decimal(5,4)数据类型中,其CHECK约束条件确保值不会超过1.0000(假设是上限)并且永远不会低于0 (假设那是地板).如果要存储其面值(例如,100.00%存储为100.00),则应使用decimal(5,2)适当的CHECK约束.结合良好的列名称,它使其他开发人员清楚地知道数据是什么以及数据如何存储在列中.
Joh*_*van 21
decimal.columnName decimal(precision, scale).精度表示数字中可以保存的总位数,刻度表示其中有多少位于小数位后面,因此decimal(3,2)是一个可以表示的数字as #.##; decimal(5,3)会的##.###. decimal并且numeric基本上是一回事.但是decimal,除非另有说明(例如,根据贵公司的编码标准),否则始终使用ANSI.示例场景
decimal(5,4).decimal(3,2).例:
if object_id('Demo') is null
create table Demo
(
Id bigint not null identity(1,1) constraint pk_Demo primary key
, Name nvarchar(256) not null constraint uk_Demo unique
, SomePercentValue decimal(3,2) constraint chk_Demo_SomePercentValue check (SomePercentValue between 0 and 1)
, SomePrecisionPercentValue decimal(5,2) constraint chk_Demo_SomePrecisionPercentValue check (SomePrecisionPercentValue between 0 and 1)
)
Run Code Online (Sandbox Code Playgroud)
进一步阅读:
0 to 1vs 0 to 100:C#:存储百分比,50还是0.50?| 归档时间: |
|
| 查看次数: |
131552 次 |
| 最近记录: |