0

Decimal value not showing correctly in html td. Here is the code:

<table>
   <tr>
       <td>@Model.ActualExpenceEntry.Allowance</td>
   </tr>
</table>

where @Model.ActualExpenceEntry.Allowance is the decimal value like 123.365. But in HTML it shows like 123,365. I want to show as it is..

Sirwan Afifi
  • 10,654
  • 14
  • 63
  • 110
sajbeer
  • 191
  • 2
  • 2
  • 10

3 Answers3

1

You may use the following:

<table>
   <tr>
       <td>@Model.ActualExpenceEntry.Allowance.ToString(CultureInfo.InvariantCulture)</td>
   </tr>
</table>

This way you force the number to be displayed in the invariant culture which among other means also that the dot (.) will be used as a decimal separator.

However you may want to set up a culture info in your web.config file so the same number format is used across the whole application - e.g.:

<configuration>
    <system.web>
        <globalization culture="en-US" uiCulture="en-US" />
    </system.web>
</configuration>

Then you of course do not need the hack with .ToString(CultureInfo.InvariantCulture)

Anton
  • 2,458
  • 2
  • 18
  • 30
0

is't depend on client culture, use configuration to solve it.

<system.web>
    <globalization culture="xx-XX" uiCulture="xx-XX" />
  </system.web>
Gurgen Sargsyan
  • 1,087
  • 15
  • 22
0

The way floating-point values (float, double, decimal) are displayed are heavily dependent on locale.

Some locales use , as decimal-separator, some use ..

There are multiple ways to solve your problem. If this is the only place in which you want to display decimals with a ., rather than a ,, then i would recommend using .ToString() with a custom format (or locale)

Example

<td>@Model.ActualExpenceEntry.Allowance.ToString("0.##")</td>

This will use the current threads culture.

Using the InvariantCulture, will also display the number with a . as decimal-separator.

<td>@Model.ActualExpenceEntry.Allowance.ToString(CultureInfo.InvariantCulture)</td>

Another way to solve the problem is to change the culture for the project globally, this however might for instance mess up number parsing and "print-outs".

For instance, for Double.Parse() will ignore , and parse "1,5" to 15. Which might be an easy thing to overlook.

@Gurgen_Sargsyan provided an example on how to do this via web.config

it's depend on client culture, use configuration to solve it.

  <system.web>
   <globalization culture="xx-XX" uiCulture="xx-XX" />
  </system.web>

Where xx-XX is a valid cutlure, for instance en-GB, or en-US. Leaving it empty will cause your application to fall back on InvariantCulture.

mausworks
  • 1,607
  • 10
  • 23