14,302,860 members

# To Remove Decimal From Price

Rate this:
29 Jun 2006
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 ;`<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>

<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>

`{`<o:p>

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

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

`}`<o:p>

`else`` `<o:p>

`{`<o:p>

`gn1 = gn;`<o:p>

`}`<o:p>

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

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

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

`sw1.WriteLine(gn3);`<o:p>

## 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 .

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

## About the Author

 Web Developer 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

 First Prev Next
 How about... Corneliu Tusnea30-Jun-06 15:53 Corneliu Tusnea 30-Jun-06 15:53
 Re: How about... msaffi3-Jul-06 21:12 msaffi 3-Jul-06 21:12
 Re: How about... Corneliu Tusnea3-Jul-06 21:23 Corneliu Tusnea 3-Jul-06 21:23
 Re: How about... msaffi3-Jul-06 21:55 msaffi 3-Jul-06 21:55
 Re: How about... Corneliu Tusnea4-Jul-06 3:23 Corneliu Tusnea 4-Jul-06 3:23
 Msaffi, I like your perfection. However it has two big flaws: 1. You use: gn = (dr["Price"].ToString()); Why would you use '.ToString()' if that is already a string? You didn't specify in your article that your price is a string in the database. 2. Now that is something strange that I've never seen in my life. Having a price kept in the database as string. Now that is original. http://www.acorns.com.au
 Re: How about... wroldan2-Nov-06 5:55 wroldan 2-Nov-06 5:55
 The easiest way by far mrsnipey9-Oct-06 14:38 mrsnipey 9-Oct-06 14:38
 Should probably use this Ennis Ray Lynch, Jr.30-Jun-06 9:15 Ennis Ray Lynch, Jr. 30-Jun-06 9:15
 Last Visit: 20-Sep-19 6:01     Last Update: 20-Sep-19 6:01 Refresh 1

General    News    Suggestion    Question    Bug    Answer    Joke    Praise    Rant    Admin

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

Article
Posted 29 Jun 2006

38.5K views
11 bookmarked