truncating a decimal to 2 decimal places??

G

GaryB

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
 
J

Just Me

There are other ways, but you could multiply by 100, truncate, then divide
by 100
 
H

Herfried K. Wagner [MVP]

* "GaryB said:
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'.
 
K

Ken Tucker [MVP]

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

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

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
 
K

Ken Tucker [MVP]

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

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

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

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top