string formatting dollar amounts

  • Thread starter Thread starter tshad
  • Start date Start date
T

tshad

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
 
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
 
Chris Dunaway said:
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
 
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 said:
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.
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
 
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.


You need to look at NumberFormatInfo.NumberNegativePattern

Where would I look for this?

Thanks,

Tom.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top