I'm doing some really simple math and saving the result to a MS SQL2008 DB.
我正在做一些非常简单的计算,并将结果保存到MS SQL2008 DB中。
I'm averaging out the some numbers, which are byte values between 1<->5. I wish to record probably 2 decimal places only. I don't care about rounding for the 2nd decimal place (eg. a 1.155 == 1.5 or 1.6 .. i'm not too phased).
我求一些数字的平均值,它们是1<->5之间的字节值。我希望只记录小数点后两位。我不关心小数点后两位的四舍五入。a 1.155 = 1.5或1.6 ..我不太分阶段)。
So .. should i store the average result as a float, decimal or double?
所以. .我应该将平均结果存储为浮点数、小数还是双精度数?
When i check what LINQ returns, it can return all three values!
当我检查LINQ返回什么时,它可以返回所有三个值!
Lastly, what would be the relevant SQL datatype field, also.
最后,还有相关的SQL数据类型字段。
cheers!
干杯!
5 个解决方案
#1
2
What you need is the DECIMAL datatype:
您需要的是十进制数据类型:
declare @val decimal(10,2)
select @val = 10.155
select @val
When you input values, you can either rely on the built in rounding, or explicitly decide which rounding you want:
当您输入值时,您可以依赖于内建的舍入,或者显式地决定您想要的舍入:
select val = round(10.155, 2, 0) -- rounded
select val = round(10.155, 2, 1) -- truncated
Decimal (10,2) means that ten digits can be used, and that two of them are to be taken as being after the decimal point. i.e. The highest number that decimal(4,2) can contain is 99.99. Trying to set it to 100 will result in arithmetic overflow.
十进制(10,2)是指可以使用10位数字,其中的2位在小数点后。也就是说,小数(4,2)可以包含的最高数是99.99。尝试将它设置为100将导致算术溢出。
#2
#3
1
Decimal is mainly use for currency. While it would work, people might find it confusing. In this case I think float would be a better choice.
十进制主要用于货币。虽然它能起作用,但人们可能会感到困惑。在这种情况下,我认为浮动是更好的选择。
#4
0
Decimal - it is the simplest, however any of mentined will do the job
十进制——它是最简单的,但是任何一个被指导过的都可以
#5
0
If you want store the results using as little space as possible you could probably do something like this (pseudocode):
如果你想用尽可能少的空间存储结果,你可以做如下的事情(伪代码):
integer = ( sum(all_values) / all_values.count().float ) * 100;
This will give you the value 352 for an average of 3.52 so you can store it as an integer (byte would be big enough I guess), and just divide it by 100 when you want to display it.
这将为您提供平均值为3.52的值352,因此您可以将其存储为一个整数(我猜字节应该足够大),并在想要显示它时将其除以100。
On the other hand if you don't care about the storage of the average value, using float values is maybe faster. (Test both to see what's actually fastest on your system.)
另一方面,如果你不关心平均值的存储,使用浮动值可能会更快。(测试两者,看看系统中什么是最快的。)
#1
2
What you need is the DECIMAL datatype:
您需要的是十进制数据类型:
declare @val decimal(10,2)
select @val = 10.155
select @val
When you input values, you can either rely on the built in rounding, or explicitly decide which rounding you want:
当您输入值时,您可以依赖于内建的舍入,或者显式地决定您想要的舍入:
select val = round(10.155, 2, 0) -- rounded
select val = round(10.155, 2, 1) -- truncated
Decimal (10,2) means that ten digits can be used, and that two of them are to be taken as being after the decimal point. i.e. The highest number that decimal(4,2) can contain is 99.99. Trying to set it to 100 will result in arithmetic overflow.
十进制(10,2)是指可以使用10位数字,其中的2位在小数点后。也就是说,小数(4,2)可以包含的最高数是99.99。尝试将它设置为100将导致算术溢出。
#2
1
I would choose Float over Double and probably Float over Decimal:. Id go for Floatalthough all should give the same result.
我选择Float / Double,可能是Float / Decimal:。我要去找浮点数,尽管所有的都应该给出相同的结果。
Have a look at these two pages for Floats & Decimals
看看这两页的浮动和小数
#3
1
Decimal is mainly use for currency. While it would work, people might find it confusing. In this case I think float would be a better choice.
十进制主要用于货币。虽然它能起作用,但人们可能会感到困惑。在这种情况下,我认为浮动是更好的选择。
#4
0
Decimal - it is the simplest, however any of mentined will do the job
十进制——它是最简单的,但是任何一个被指导过的都可以
#5
0
If you want store the results using as little space as possible you could probably do something like this (pseudocode):
如果你想用尽可能少的空间存储结果,你可以做如下的事情(伪代码):
integer = ( sum(all_values) / all_values.count().float ) * 100;
This will give you the value 352 for an average of 3.52 so you can store it as an integer (byte would be big enough I guess), and just divide it by 100 when you want to display it.
这将为您提供平均值为3.52的值352,因此您可以将其存储为一个整数(我猜字节应该足够大),并在想要显示它时将其除以100。
On the other hand if you don't care about the storage of the average value, using float values is maybe faster. (Test both to see what's actually fastest on your system.)
另一方面,如果你不关心平均值的存储,使用浮动值可能会更快。(测试两者,看看系统中什么是最快的。)