469,265 Members | 1,994 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,265 developers. It's quick & easy.

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 23537
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)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
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,

Sorry bad example.

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.

Similar topics

8 posts views Thread by nick | last post: by
5 posts views Thread by VISHNU VARDHAN REDDY UNDYALA | last post: by
3 posts views Thread by cj | last post: by
2 posts views Thread by prakashdehury | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.