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

returns "0"

While I was expecting it to return

"0.00"
3
  • 1
    I think the problem here is that var has explicitly typed amount as a string, get rid of the quotes. Commented May 29, 2012 at 11:50
  • You are printing the string "0". Its representation is "0", try printing decimal amount = 0; Commented May 29, 2012 at 11:50
  • why is this question being up-voted? it's a simple programming mistake. ho-hum. Commented May 29, 2012 at 12:00

3 Answers 3

10

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;
Sign up to request clarification or add additional context in comments.

Comments

0

Try:

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

OR

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

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.