471,888 Members | 2,023 Online

# truncating a decimal to 2 decimal places??

I have a database field that is the product of a multiply in a sql statement
and it sometimes has 5 or more decimal places. I want to truncate beyond
the 2nd decimal place(###.##). I figured a Ctype to a decimal value would
do that but the four decimal places remain. I found a truncate function but
it only truncates to whole numbers. how can I do the truncation?
Thanks,
G
Nov 21 '05 #1
5 23764
There are other ways, but you could multiply by 100, truncate, then divide
by 100
"GaryB" <gb@nospam.com> wrote in message
news:OW**************@TK2MSFTNGP15.phx.gbl...
I have a database field that is the product of a multiply in a sql
statement
and it sometimes has 5 or more decimal places. I want to truncate beyond
the 2nd decimal place(###.##). I figured a Ctype to a decimal value would
do that but the four decimal places remain. I found a truncate function
but
it only truncates to whole numbers. how can I do the truncation?
Thanks,
G

Nov 21 '05 #2
* "GaryB" <gb@nospam.com> scripsit:
I have a database field that is the product of a multiply in a sql statement
and it sometimes has 5 or more decimal places. I want to truncate beyond
the 2nd decimal place(###.##). I figured a Ctype to a decimal value would
do that but the four decimal places remain. I found a truncate function but
it only truncates to whole numbers. how can I do the truncation?

Take a look at 'Math.Round', 'Math.Floor', and 'Math.Ceiling'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #3
" Just Me" <ne********@a-znet.com> wrote in
news:O7**************@TK2MSFTNGP12.phx.gbl:
There are other ways, but you could multiply by 100, truncate, then
divide by 100

Just use Math.round.

--
Lucas Tam (RE********@rogers.com)
Nov 21 '05 #4
Hi,

Take a look at the numberformatinfo class.
Dim decTest As Decimal = CDec(5.0001)

Dim niDec As New System.Globalization.NumberFormatInfo

niDec.NumberDecimalDigits = 4

Debug.WriteLine(decTest.ToString(niDec))

Ken

-----------------------

"GaryB" <gb@nospam.com> wrote in message
news:OW**************@TK2MSFTNGP15.phx.gbl...
I have a database field that is the product of a multiply in a sql statement
and it sometimes has 5 or more decimal places. I want to truncate beyond
the 2nd decimal place(###.##). I figured a Ctype to a decimal value would
do that but the four decimal places remain. I found a truncate function but
it only truncates to whole numbers. how can I do the truncation?
Thanks,
G

Nov 21 '05 #5
Hi,

Dim decTest As Decimal = CDec(5.0001)

Dim niDec As New System.Globalization.NumberFormatInfo

niDec.NumberDecimalDigits = 2

Debug.WriteLine(decTest.ToString("f", niDec))

Ken

----------------

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:eF**************@TK2MSFTNGP12.phx.gbl...
Hi,

Take a look at the numberformatinfo class.
Dim decTest As Decimal = CDec(5.0001)

Dim niDec As New System.Globalization.NumberFormatInfo

niDec.NumberDecimalDigits = 4

Debug.WriteLine(decTest.ToString(niDec))

Ken

-----------------------

"GaryB" <gb@nospam.com> wrote in message
news:OW**************@TK2MSFTNGP15.phx.gbl...
I have a database field that is the product of a multiply in a sql statement
and it sometimes has 5 or more decimal places. I want to truncate beyond
the 2nd decimal place(###.##). I figured a Ctype to a decimal value would
do that but the four decimal places remain. I found a truncate function but
it only truncates to whole numbers. how can I do the truncation?
Thanks,
G

Nov 21 '05 #6

### This discussion thread is closed

Replies have been disabled for this discussion.