Click here to Skip to main content
Click here to Skip to main content
Go to top

To Remove Decimal From Price

, 29 Jun 2006
Rate this:
Please Sign up or sign in to vote.
How to remove decimal from Price column where , decimal point exist

Introduction

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 ;

sw1 = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory)+"\\Temp.txt"); 

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 .

 

string gn,gn1,gn2 ;

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.

i = gn.IndexOf(".");  // This line will return place of decimal"." from string gn 

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 )

{

gn1 = gn.Remove(i, gn.Length - i);

gn2 = gn.Remove(0,i+1);

}

else

{

gn1 = gn;

}

gn1 = gn1.PadLeft(5,'0');

gn2 = gn2.PadRight(6,'0');

gn3 = string.Concat(gn1,gn2);

sw1.WriteLine(gn3);

 

Conclusion

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 .

License

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

Share

About the Author

msaffi
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 PinmemberPIEBALDconsult14-May-09 14:54 
QuestionHow about... PinmemberCorneliu Tusnea30-Jun-06 15:53 
AnswerRe: How about... Pinmembermsaffi3-Jul-06 21:12 
GeneralRe: How about... PinmemberCorneliu Tusnea3-Jul-06 21:23 
GeneralRe: How about... Pinmembermsaffi3-Jul-06 21:55 
GeneralRe: How about... PinmemberCorneliu Tusnea4-Jul-06 3:23 
GeneralRe: How about... Pinmemberwroldan2-Nov-06 5:55 
AnswerThe easiest way by far Pinmembermrsnipey9-Oct-06 14:38 
GeneralShould probably use this PinmemberEnnis Ray Lynch, Jr.30-Jun-06 9:15 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 30 Jun 2006
Article Copyright 2006 by msaffi
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid