Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
How do I take a number like maybe 0.6667 and remove the decimal place and making it a number like 0.66. In other words I have to remove the decimal.
Posted
Comments
Sandeep Mewara 18-May-11 10:38am    
Not clear.

How did you rounded 0.6667 to 0.66? What kind of rounding is this? and what do you mean by remove the decimal place?

SQL
DECLARE @decimal decimal(6, 6)
SET @decimal = 0.6667


--EASY WAY (with rounding)
DECLARE @decimal2 decimal(6,2)
SET @decimal2 = @decimal

PRINT @decimal2


--SEMI-EASY WAY (no rounding)
--Not dependable unless you have a set length for all the INPUT numbers
DECLARE @vchar_decimal varchar(4)
SET @vchar_decimal = CAST(@decimal as varchar(10))

PRINT @vchar_decimal


--HARD WAY (no rounding)
DECLARE @char_decimal varchar(MAX)
DECLARE @decimal_position int
DECLARE @decimal_output varchar(50)

SET @char_decimal = CAST(@decimal as varchar(MAX))
SET @decimal_position = CHARINDEX('.', @char_decimal)
SET @decimal_output = SUBSTRING(@char_decimal, 1, (@decimal_position + 2))

PRINT @decimal_output



Hope this helps,
-Artificer GM
 
Share this answer
 
v2
SQL
declare @DecimalVal Decimal(18,7)
set @DecimalVal=14.5267
select @DecimalVal
 select CONVERT(DECIMAL(18,2),CAST(cast(@DecimalVal* 100 as int) as decimal (18, 2))/100)
 
Share this answer
 
v2
Comments
King Fisher 29-May-14 5:08am    
good Job !! but It's 3 Years Old Question
You can use a format function.
 
Share this answer
 
in .Net you can use like this.

Math.Floor(0.6667*100)/100
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900