473,385 Members | 1,587 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,385 software developers and data experts.

Format question

OC
What would be the best way to check for a decimal number coming in as, for
example, 99999.9999 and truncating it to 99999.99?

IOW, if it is 4 digits after the decimal, truncate it to 2.
Nov 15 '05 #1
4 2875
"OC" <nf*@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
What would be the best way to check for a decimal number coming in as, for
example, 99999.9999 and truncating it to 99999.99?

IOW, if it is 4 digits after the decimal, truncate it to 2.


You could probably fiddle around with string formatting, but for most
decimal numbers, this should work fine:

decimal dec = 99999.9999M;
dec = Decimal.Truncate(dec * 100) / 100;
MessageBox.Show(dec.ToString());

Erik
Nov 15 '05 #2
Hello

decimal d = 99999.9999M;
string s = d.ToString("0.00");

but this would format 1.1 as 1.10 and 4 as 4.00

Best regards
Sherif

"OC" <nf*@nospam.com> wrote in message
news:#6**************@TK2MSFTNGP11.phx.gbl...
What would be the best way to check for a decimal number coming in as, for
example, 99999.9999 and truncating it to 99999.99?

IOW, if it is 4 digits after the decimal, truncate it to 2.

Nov 15 '05 #3
"Sherif ElMetainy" <el*************@wayout.net.NOSPAM> wrote in message
news:ey***************@TK2MSFTNGP11.phx.gbl...
Hello

decimal d = 99999.9999M;
string s = d.ToString("0.00");

but this would format 1.1 as 1.10 and 4 as 4.00

Best regards
Sherif


That doesn't truncate - it rounds. The result in string s is
"100000.00", not "99999.99".

Erik
Nov 15 '05 #4
You could use regular expressions to analyze and get groups of digits before and after the "." sign and then appropriately truncate your decimal part.

--
Cezary Nolewajka
mailto:c.*********************@no-sp-am-eh-mail.com
remove all "no-sp-am-eh"s to reply
"OC" <nf*@nospam.com> wrote in message news:%2****************@TK2MSFTNGP11.phx.gbl...
What would be the best way to check for a decimal number coming in as, for
example, 99999.9999 and truncating it to 99999.99?

IOW, if it is 4 digits after the decimal, truncate it to 2.

Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dario Di Bella | last post by:
Hi all, we have the following urgent issue affecting our development team. Initially we had one particular workstation that failed executing queries on a DB2 database, raising an invalid date...
1
by: Tom | last post by:
In the Developers Handbook, Getz provides a way to create a survey report that displays either a line, Yes/No or multiple choice for each question. It's done by placing three controls (one for each...
7
by: Tommy Vercetti | last post by:
The first three of these statements work. The fourth doesn't. String::Format("{0}{0}{0}{0}", S""); String::Format("{0}{1}{0}{0}", S"", S""); String::Format("{0}{1}{2}{0}", S"", S"", S"");...
16
by: Al Reid | last post by:
First, I'm using vb2005. I have a string that is read from a barcode reader into a TextBox. The string is 6 characters long and represents a date (mmddyy). I want to display it to the user in a...
2
by: gjuro kladaric | last post by:
hi there, I am just learning about IFormattable, ICustomFormatter, IFormatProvider and the like... although being a bit tricky to understand, it becomes clear after some clicking and...
13
by: Roy | last post by:
Hi all, I'm creating a project that should always use this date format when displays the dates or create dates. The back end database is a SQL Server and I like to know what is the logical way...
5
by: veaux | last post by:
I'm thinking this is easy but can't get it. I have a table with following: Table1 Date 1/1/2007 Table2 Type 0107 (This is MMYY of above) So I'm having trouble using a query to turn the...
10
by: ARC | last post by:
Hello all, General question for back-end database that has numerous date fields where the database will be used in regions that put the month first, and regions that do not. Should I save a...
12
by: param | last post by:
Hi All, I want to use %x as directive to get the locale specific date representation. But, at the same time, i want to know the format of the output for interpreting it through program. Is there...
9
by: Martin | last post by:
I'm retrieving some records from a database. One of the fields contains a date/time. I would like to format it as I send it out to the table in the displayed page. Can some one please tell me...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.