写一个带有两位小数的数字 SQL Server
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/441600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Write a number with two decimal places SQL server
提问by
How do you write a number with two decimal places for sql server?
你如何为sql server写一个小数点后两位的数字?
回答by
try this
尝试这个
SELECT CONVERT(DECIMAL(10,2),YOURCOLUMN)
回答by Charles Bretana
Use Str()
Function. It takes three arguments(the number, the number total characters to display, and the number of decimal places to display
使用Str()
功能。它需要三个参数(数字、要显示的总字符数和要显示的小数位数)
Select Str(12345.6789, 12, 3)
displays: ' 12345.679' ( 3 spaces, 5 digits 12345, a decimal point, and three decimal digits (679). - it rounds if it has to truncate, (unless the integer part is too large for the total size, in which case asterisks are displayed instead.)
显示:'12345.679'(3 个空格、5 个数字 12345、一个小数点和三个十进制数字 (679)。 - 如果它必须截断,它会四舍五入,(除非整数部分对于总大小来说太大,在这种情况下而是显示星号。)
for a Total of 12 characters, with 3 to the right of decimal point.
共 12 个字符,小数点右侧 3 个字符。
回答by AAA
Generally you can define the precision of a number in SQL by defining it with parameters. For most cases this will be NUMERIC(10,2)
or Decimal(10,2)
- will define a column as a Number with 10 total digits with a precision of 2 (decimal places).
通常,您可以通过使用参数定义数字来定义 SQL 中数字的精度。在大多数情况下,这将是NUMERIC(10,2)
或Decimal(10,2)
- 将一列定义为一个总位数为 10 位且精度为 2(小数位)的数字。
Edited for clarity
为清晰起见进行了编辑
回答by Mohamed Ramadan
This work for me and always keeps two digits fractions
这对我有用并且总是保持两位数的分数
23.1 ==> 23.10
23.1 ==> 23.10
25.569 ==> 25.56
25.569 ==> 25.56
1 ==> 1.00
1 ==> 1.00
Cast(CONVERT(DECIMAL(10,2),Value1) as nvarchar) AS Value2
回答by ClubbieTim
This is how the kids are doing it today:
这就是今天孩子们的做法:
DECLARE @test DECIMAL(18,6) = 123.456789
SELECT FORMAT(@test, '##.##')
123.46
123.46
回答by SonalPM
If you only need two decimal places, simplest way is..
如果你只需要两位小数,最简单的方法是..
SELECT CAST(12 AS DECIMAL(16,2))
OR
或者
SELECT CAST('12' AS DECIMAL(16,2))
Output
输出
12.00
回答by antoine
If you're fine with roundingthe number instead of truncating it, then it's just:
如果您可以对数字进行四舍五入而不是截断它,那么它只是:
ROUND(column_name,decimals)
回答by Anil Chaudhary
Try this:
尝试这个:
declare @MyFloatVal float;
set @MyFloatVal=(select convert(decimal(10, 2), 10.254000))
select @MyFloatVal
Convert(decimal(18,2),r.AdditionAmount) as AdditionAmount
回答by kumar vishwash
This will allow total 10 digits with 2 values after the decimal. It means that it can accomodate the value value before decimal upto 8 digits and 2 after decimal.
这将允许总共 10 位数字,小数点后有 2 个值。这意味着它可以容纳小数点前最多 8 位和小数点后 2 位的值。
To validate, put the value in the following query.
要进行验证,请将值放入以下查询中。
DECLARE vtest number(10,2);
BEGIN
SELECT 10.008 INTO vtest FROM dual;
dbms_output.put_line(vtest);
END;
回答by SQL Master
Multiply the value you want to insert (ex. 2.99) by 100
将要插入的值(例如 2.99)乘以 100
Then insert the division by 100 of the result adding .01 to the end:
然后插入除以 100 的结果加上 0.01 到最后:
299.01/100