Click here to Skip to main content
15,443,630 members
Articles / Programming Languages / C#
Posted 29 Jun 2006


11 bookmarked

To Remove Decimal From Price

Rate me:
Please Sign up or sign in to vote.
1.00/5 (18 votes)
29 Jun 20063 min read
How to remove decimal from Price column where , decimal point exist


As This is my first article over here on code project .In the life of developing softwares, sometimes we couldnt make things properly worked . For example whatever we think is simple is not simple for us or whatever we think difficult is not difficult for us .It all depends upon the persons mental approach  and Logic for solving .

Description of Article

Today I going to discuss the basic thing for removing decimal points from the Price. Because I have spend like 4 hours just to solve this matter . So I thought to discuss this basics with others who will not want to spend 4 hours just to solve this problem. Because I am working with company who used to develop products for stock excahnge and financial Institutes , And my job is very critical to make EOD or END OF DAY Files. This involves very much familarity with the Database design & schema with relations . Basically The files Needed for firms at the end of day for Settlement & record keeping of all database fields which they have used for stocks buying or selling etc.. So EOD file is to be character specific means every field should be cheked with its length constraints, the fields which are to be taken from DB are all accoridng to the firms format should be different for different firms or companys. My problem is that I need all numerical values to be converted to a positive integer.
(i.e. if I find price for "19.99" to be converted to "1999") with leading and traling by zeros .For that I Have Solved My problem and want others who get irritated just to solve again this thing find a solution that will check a numerical value to see if a decimal point is present, and if it is, then to remove it. 

Solution To Problem

The solution to above problem for some developers is just to multiply Decimal values with 100 to get these kinda values to be converted to a positive integer.  If we want Price This (19.99  or 56.23)  to This(1999 or 5623) , just multiply by 100 . But My problem is to get values in Positive integer with leading and traling by zeros , Like for example if I have value 0.23456 and 504.34678 etc .  so My end result will be like 00000230000 and 00504346780 As in my scenario I have price constraint to be 11 characters long without decimal point and the format should be like , nnnnn.nnnnnn(without decimal point equal to 11 characters long).

 I have to write Data to file , for that I used Stream Writer. First Initialize Stream writer.

Private StreamWriter sw1 ;<o:p>

sw1 = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory)+"\<A href="file://Temp.txt/">\Temp.txt</A><A href="file://Temp.txt/">");  </A><o:p>

As I want to create text file in projects Debug Folder, You can specify any path for creating text file .I am omitting most of the things like connection string ,Dataset,DataAdapter etc. But as To discuss the problem for removing decimal point from price. Take Strings gn,gn1,gn2 and int i .<o:p>


string gn,gn1,gn2 ;<o:p>

int i;

in my case I have taken values from DB , for filed Price .so for taking values from DB in gn.

gn = (dr["Price"].ToString());   // you can used hard coded string as well , where "dr" is DataRow.<o:p>

i = gn.IndexOf(".");  // This line will return place of decimal"." from string gn <o:p>

As indexof fuction gives numeric values , if i = -1 , it means there is no decimal within string or if i = 1, then it means decimal is after first place ,if  i = 2 ,then it means after second place etc. 

if( i > 0 )<o:p>


gn1 = gn.Remove(i, gn.Length - i);<o:p>

gn2 = gn.Remove(0,i+1);<o:p>


else <o:p>


gn1 = gn;<o:p>


gn1 = gn1.PadLeft(5,'0');<o:p>

gn2 = gn2.PadRight(6,'0');<o:p>

gn3 = string.Concat(gn1,gn2);<o:p>




Hope so this article will help those who will be getting Upset in a same situation . Although this article is for beginer level but as i found intresting to post here. Hope so You will enjoy this. And better to feed back your comments. I am Muhammad saffi Hussain . Working as Developer &  Analyst in Dubai & pakistan .


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Written By
Web Developer
United Kingdom United Kingdom
I have Done BS Computer Science and involved with a 2 yrs experience in varoius areas related to IT domain . Currently working with Product based development.

Comments and Discussions

QuestionHow about this Pin
PIEBALDconsult14-May-09 14:54
professionalPIEBALDconsult14-May-09 14:54 
QuestionHow about... Pin
Corneliu Tusnea30-Jun-06 15:53
MemberCorneliu Tusnea30-Jun-06 15:53 
int price = ...
string normalized = price.ToString( "000000000.00000" ).Replace(".",""); ?
AnswerRe: How about... Pin
msaffi3-Jul-06 21:12
Membermsaffi3-Jul-06 21:12 
GeneralRe: How about... Pin
Corneliu Tusnea3-Jul-06 21:23
MemberCorneliu Tusnea3-Jul-06 21:23 
GeneralRe: How about... Pin
msaffi3-Jul-06 21:55
Membermsaffi3-Jul-06 21:55 
GeneralRe: How about... Pin
Corneliu Tusnea4-Jul-06 3:23
MemberCorneliu Tusnea4-Jul-06 3:23 
GeneralRe: How about... Pin
wroldan2-Nov-06 5:55
Memberwroldan2-Nov-06 5:55 
AnswerThe easiest way by far Pin
mrsnipey9-Oct-06 14:38
Membermrsnipey9-Oct-06 14:38 
GeneralShould probably use this Pin
Ennis Ray Lynch, Jr.30-Jun-06 9:15
MemberEnnis Ray Lynch, Jr.30-Jun-06 9:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.