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

返回"0"

虽然我期待它回来

"0.00"
4

3 回答 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;
于 2012-05-29T11:50:17.193 回答
0

Try:

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

OR

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

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

于 2012-05-29T11:50:18.773 回答
0

尝试这个

.ToString("N2") 

它将使用 CultureInfo 来格式化数字。这意味着您的千位分隔符可能会有所不同,具体取决于使用的 CultureInfo。如果需要,您还可以传递所需的 CultureInfo。

于 2012-05-29T11:52:45.860 回答