473,388 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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 23922
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
2
by: Carl G | last post by:
I am storing a 0.000 a System.Decimal in a DataRow. On retrieval the value is only 0 without the three decimal places. It looks like the Get property returns System.Decimal.Zero, but why???? I...
4
by: italia | last post by:
I changed the Fieldsize Property from text to Long Integer and Decimal Places = 6. I had decimals in the original field. But after the transfer, the digits after the decimals are gone. Now...
8
by: nick | last post by:
printf("%lf",3.25); the result is 3.25000 i want the answer correct to 3 decimal places What should i do? thanks!
5
by: VISHNU VARDHAN REDDY UNDYALA | last post by:
Hello, Can someone over here help me in truncating a float variable. I mean if PI=3.14159 ...How can I get to read the first two or first three decimal values with out rounding them. Any...
3
by: cj | last post by:
I want 7 * .105263 to result in an int32 value of 0. and 7 * .189021 to result in an int32 value of 1. I don't want to round. How can I just drop the decimal places?
2
by: prakashdehury | last post by:
can some one pls suggest me why? StreamWriter sw = new StreamWriter(@"c:\Temp\1.txt"); double dd1; double dd2; double dd3; double dd4; dd1 = 1452.123456789876123456; dd2 =...
4
by: Todd | last post by:
I have a function that takes a double and truncates it to a specified number of decimal places. I've found that I have to add a small number to the input value otherwise I get errors due to the...
1
by: Serenityquinn15 | last post by:
Hi! I have a little trouble about in truncating numbers. What i want to do is to get the two decimal places. here's the example: when i try to get the half of 20500.39 it shows like...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.