Formating String Number Decimal places

B

Bill

Hi
I am having trouble trying to format a number string to two decimal places
if the number ends in a 0
It formats fine otherwise.

For example if PrincRepay is 78.20 it will display it as 78.2
PrincRepay, payment and IntAmt are double

PrincRepay = Math.Round(payment - IntAmt,2);
PrincRepayStr = PrincRepay.ToString("N2");

Is there anyway around this

Thanks in advance
Bill
 
G

Guest

Hi Bill,

Try this:

System.Globalization.NumberFormatInfo nfi = new
System.Globalization.NumberFormatInfo();
nfi.NumberDecimalDigits = 2;

double i = 78.2;
string s = i.ToString("N", nfi);
Console.WriteLine(s); // Will output 78.20

Regards,

Pete
 
B

Bill

Hi Pete

I tried this however I received the same result

System.Globalization.NumberFormatInfo nfi = new

System.Globalization.NumberFormatInfo();

nfi.NumberDecimalDigits = 2;



PrincRepayStr = PrincRepay.ToString("N", nfi);

Bill
 
G

Guest

Hi Bill,

Perhaps you are running a different CurrentCulture that I am. Try the code
below which specifies the culture. If this does not work, maybe the problem
is somewhere else along the line.

System.Globalization.NumberFormatInfo nfi = new
System.Globalization.CultureInfo("en-US", false).NumberFormat;
nfi.NumberDecimalDigits = 2;

double PrincRepay = 78.2;
string PrincRepayStr = PrincRepay.ToString("N", nfi);

// PrincRepayStr is "78.20"

Regards,

Peter
 
G

Guest

Hi Bill,
I am not sure why I get this and you don't but if I do the following:

double d = 78.2;
Console.WriteLine(d.ToString("N2"));

Then I get 78.20 printed on the Console.
 
P

PW

Mark R. Dawson said:
Hi Bill,
I am not sure why I get this and you don't but if I do the following:

double d = 78.2;
Console.WriteLine(d.ToString("N2"));

Then I get 78.20 printed on the Console.

This works for me as well. You might try:

d.ToString("0.00")

Although I suspect there is something else fouling up your code.
 

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

Top