457,911 Members | 1,179 Online Need help? Post your question and get tips & solutions from a community of 457,911 IT Pros & Developers. It's quick & easy.

# string formatting dollar amounts

 P: n/a I need to convert a number I get from my Sql table (where the value is a string) to a string and have it be a precision of 2 and I want to reverse the sign. I originally used: String.Format({0:c},dr["Amount"]) but then I needed to reverse the sign. The problem is that if the value is negative, I get the value with a "()" around it, such that "-15.00", becomes (15.00). I got an error when I tried to set it to negative. So I tried: String.Format({0:#.##),dr["Amount"]) But that would take a value of 15.35 and make a value of 15.3500. I thought the #.## would make it double precision, but it doesn't. How do I handle this? Thanks, Tom Apr 25 '06 #1
7 Replies

 P: n/a Try using the decimal data type: int i = 0; //Set this to the column number you want to retrieve //Using an index to the column is faster that specifying the column name. Decimal d = dr.GetDecimal(i); //Then use ToString to convert to currency: String s = d.ToString("C2"); Hope this gives you some ideas Apr 25 '06 #2

 P: n/a "Chris Dunaway" wrote in message news:11**********************@g10g2000cwb.googlegr oups.com... Try using the decimal data type: int i = 0; //Set this to the column number you want to retrieve //Using an index to the column is faster that specifying the column name. Decimal d = dr.GetDecimal(i); This almost works. If i = 600, d will be equal 600 (not 600.00). So we need to do the next line. //Then use ToString to convert to currency: String s = d.ToString("C2"); This will make s = (\$600.00). This is a problem. I don't want the parens. I wanted -600.00 or -\$600.00 where I take out the \$ using regex.replace. I suppose I could also replace the parens in this way, but I would prefer it to be come back -600.00. if d = 600 String.Format("{0:#.##},d) would equal 600 (why doesn't it = 600.00)? I thought maybe String.Format("{0:#.00},d) would do it, but it was still 600. Thanks, Tom Hope this gives you some ideas Apr 25 '06 #3

 P: n/a I don't understand why the format strings don't work. From what I read they should. If I have decimal dtemp = 25.50; string stemp; These work: stemp = dtemp.ToString("C2"); stemp = String.Format("{0:#.##}",dtemp); stemp = String.Format("{0:#.00}",dtemp); These don't work and I get an error: "Format specifier was invalid.". stemp = dtemp.ToString("D"); stemp = dtemp.ToString("D2"); stemp = dtemp.ToString("d2"); stemp = dtemp.ToString("d2"); stemp = String.Format("{0:d}",dtemp); stemp = String.Format("{0:d2}",dtemp); stemp = String.Format("{0:D}",dtemp); stemp = String.Format("{0:D2}",dtemp); Tom "tshad" wrote in message news:u\$**************@TK2MSFTNGP03.phx.gbl... "Chris Dunaway" wrote in message news:11**********************@g10g2000cwb.googlegr oups.com... Try using the decimal data type: int i = 0; //Set this to the column number you want to retrieve //Using an index to the column is faster that specifying the column name. Decimal d = dr.GetDecimal(i); This almost works. If i = 600, d will be equal 600 (not 600.00). So we need to do the next line. //Then use ToString to convert to currency: String s = d.ToString("C2"); This will make s = (\$600.00). This is a problem. I don't want the parens. I wanted -600.00 or -\$600.00 where I take out the \$ using regex.replace. I suppose I could also replace the parens in this way, but I would prefer it to be come back -600.00. if d = 600 String.Format("{0:#.##},d) would equal 600 (why doesn't it = 600.00)? I thought maybe String.Format("{0:#.00},d) would do it, but it was still 600. Thanks, Tom Hope this gives you some ideas Apr 25 '06 #4

 P: n/a tshad wrote: This almost works. If i = 600, d will be equal 600 (not 600.00). So we need to do the next line. Do you have 600 fields in your database table? i is the index of the database field not the number to be converted. //Then use ToString to convert to currency: String s = d.ToString("C2"); This will make s = (\$600.00). This is a problem. I don't want the parens. I wanted -600.00 or -\$600.00 where I take out the \$ using regex.replace. I suppose I could also replace the parens in this way, but I would prefer it to be come back -600.00. You need to look at NumberFormatInfo.NumberNegativePattern Apr 25 '06 #5

 P: n/a D2, according to the docs "is supported for integral types only." Apr 25 '06 #6

 P: n/a "Chris Dunaway" wrote in message news:11*********************@v46g2000cwv.googlegro ups.com... tshad wrote: This almost works. If i = 600, d will be equal 600 (not 600.00). So we need to do the next line. My mistake, I meant d = 600, d will be equal 600 (not 600.00). Do you have 600 fields in your database table? i is the index of the database field not the number to be converted. > > //Then use ToString to convert to currency: > > String s = d.ToString("C2"); This will make s = (\$600.00). This is a problem. I don't want the parens. I wanted -600.00 or -\$600.00 where I take out the \$ using regex.replace. I suppose I could also replace the parens in this way, but I would prefer it to be come back -600.00. You need to look at NumberFormatInfo.NumberNegativePattern Where would I look for this? Thanks, Tom. Apr 27 '06 #7

 P: 7 I don't know if you are using .NET 2.0 MSDN help, I'm assuming you are (the information is basically the same in .NET 1.1, but different web links). It looks like easiest way for you would be to override the current CultureInfo.NumberFormat property. You can set both the numbernegativepattern as well as the currencynegativepattern (not sure which one you're using). http://msdn2.microsoft.com/en-us/lib...roverride.aspx http://msdn2.microsoft.com/en-us/lib...fo(VS.80).aspx http://msdn2.microsoft.com/en-us/lib...berformat.aspx http://msdn2.microsoft.com/en-us/lib...berformat.aspx http://msdn2.microsoft.com/en-us/lib...vepattern.aspx HTH...! Apr 27 '06 #8

### This discussion thread is closed

Replies have been disabled for this discussion. 