KingRichard3
Programmer
Is there an easy way to convert
a double: 1234.56
to this: $1,234.56
?
Thanks,
Rick
a double: 1234.56
to this: $1,234.56
?
Thanks,
Rick
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Select Cast(<intVariable> as money) as Amount
From table
select '$'+ Cast(price as varchar(10))
from titles
Select Convert(money, intvalue, 1) from tablename
DECLARE @num decimal(6, 2)
SET @num = 1234.56
SELECT '$' + CONVERT(varchar(20), CAST(@num AS money), 1)
Having 4 decimals is the best way to work with monetary amounts. It would be a mistake to use only 2 decimals.mrdenny said:The only problem that I have with the money data type is that is has 4 decimal places, not 2. This can throw off calculations.
[Blue]DECLARE[/Blue] @m [Blue]AS[/Blue] [Blue]money[/Blue]
[Blue]DECLARE[/Blue] @m10 [Blue]AS[/Blue] [Blue]money[/Blue]
[Blue]DECLARE[/Blue] @n10 [Blue]AS[/Blue] [Blue]numeric[/Blue][Gray]([/Gray]10[Gray],[/Gray]2[Gray])[/Gray]
[Blue]DECLARE[/Blue] @a [Blue]AS[/Blue] [Blue]money[/Blue]
[Blue]DECLARE[/Blue] @n [Blue]AS[/Blue] [Blue]numeric[/Blue][Gray]([/Gray]10[Gray],[/Gray]2[Gray])[/Gray]
[Blue]DECLARE[/Blue] @i [Blue]AS[/Blue] [Blue]int[/Blue]
[Blue]SET[/Blue] @a[Gray]=[/Gray]12.53
[Blue]SET[/Blue] @m[Gray]=[/Gray]0
[Blue]SET[/Blue] @n[Gray]=[/Gray]0
[Blue]SET[/Blue] @m10[Gray]=[/Gray]0
[Blue]SET[/Blue] @n10[Gray]=[/Gray]0
[Blue]SET[/Blue] @i[Gray]=[/Gray]1
[Blue]WHILE[/Blue] @i[Gray]<[/Gray][Gray]=[/Gray]100 [Blue]BEGIN[/Blue]
[Blue]SET[/Blue] @m10[Gray]=[/Gray]@m10[Gray]+[/Gray]@a[Gray]*[/Gray].1
[Blue]SET[/Blue] @n10[Gray]=[/Gray]@n10[Gray]+[/Gray]@a[Gray]*[/Gray].1
[Blue]SET[/Blue] @n[Gray]=[/Gray]@n[Gray]+[/Gray]@a
[Blue]SET[/Blue] @m[Gray]=[/Gray]@m[Gray]+[/Gray]@a
[Blue]SET[/Blue] @i[Gray]=[/Gray]@i[Gray]+[/Gray]1
[Blue]END[/Blue]
[Blue]SELECT[/Blue] @n[Gray]*[/Gray].1[Gray],[/Gray]@n10[Gray],[/Gray]@m[Gray]*[/Gray].1[Gray],[/Gray]@m10
[Blue]SELECT[/Blue] [Fuchsia]SUM[/Fuchsia][Gray]([/Gray]SalesAmount[Gray])[/Gray][Gray]*[/Gray].1[Gray],[/Gray] [Fuchsia]SUM[/Fuchsia][Gray]([/Gray]SalesAmount[Gray]*[/Gray].1[Gray])[/Gray]
ALTER Function FormatNumber
(@RawNumber numeric(32,2) = 0,
@ShowDecimal as bit = 0,
[COLOR=red]
@CurrencySymbol as char(1) = ''
[/color]
)
returns varchar(200) AS
BEGIN
declare @FormattedNumber as varchar(100)
declare @AfterDecimal as varchar(100)
declare @i as int
declare @j as int
set @i = 0
set @j = 0
set @FormattedNumber = convert(bigint, @RawNumber)
set @AfterDecimal = @RawNumber
if charindex('.', @AfterDecimal) <> 0
BEGIN
set @AfterDecimal = '.' + right(@AfterDecimal, len(@AfterDecimal)-charindex('.', @AfterDecimal))
END
ELSE
BEGIN
set @AfterDecimal = ''
END
while @i <> len(@FormattedNumber)
BEGIN
if @j = 3
BEGIN
set @j = -1
set @FormattedNumber = left(@FormattedNumber, len(@FormattedNumber)-@i) + ',' + right(@FormattedNumber, @i)
END
set @j = @j + 1
set @i = @i + 1
END
[COLOR=red]
IF @currencysymbol != ''
set @FormattedNumber = @currencysymbol + @FormattedNumber
[/color]
if @ShowDecimal <> 0
set @FormattedNumber = @FormattedNumber + @AfterDecimal
return @FormattedNumber
END