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>
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>
gn1 = gn;<o:p>
gn1 = gn1.PadLeft(5,'0');<o:p>
gn2 = gn2.PadRight(6,'0');<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 .
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.