Rab
Rab

Reputation: 35572

string.format issue in MVC

 var amount="0";
 @String.Format("{0:0.00}", amount)

returns "0"

While I was expecting it to return

"0.00"

Upvotes: 0

Views: 1329

Answers (3)

Imran Rashid
Imran Rashid

Reputation: 3492

Try this

.ToString("N2") 

It will use the CultureInfo to format the number. This means that your thousands separator might be different depending on the used CultureInfo. You can also pass the desired CultureInfo if you want.

Upvotes: 0

Paddy
Paddy

Reputation: 33857

Try:

String.Format("{0:#.##}", amount)

OR

String.Format("{0:N2}", amount)

Scratch this - Guffa's answer is correct...

Upvotes: 0

Guffa
Guffa

Reputation: 700342

Formatting a string will just return the string itself, you have to format a number to get it formatted as a number:

var amount = 0;

A variable with implicit type which is assigned an integer value will be an integer, so it won't have a fractional part. You might want to specify the type:

double amount = 0;

Or use a double value:

var amount = 0.0;

Upvotes: 10

Related Questions