Search code examples
c#htmlserver-side

FireFox Removes My Trailing '0'


I have a web page which in some fields displays values stored on a server but if any other these end with '0' or '.00' it removes them so £1.10 would be £1.1 and £10.00 would be £10.

In my code I have them as Decimals so I would have expected it to show the entire values. If I use Chrome or IE it works fine for me, my issue is just with FF.

In my view

<%
    Decimal sum = 0;
    Decimal totalTrancheValues = 0;
    var isCappeddrawdown = false;

    foreach (var tranche in benefitDetails.Tranches)
    {
        //Adds all remaining incomes for 'Capped drawdown' and give a total for them all
        if (tranche.FundType == "Capped drawdown")
        {
            sum = sum + Convert.ToDecimal(tranche.GrossIncomeRemainingThisPensionYear == null ? 0 : tranche.GrossIncomeRemainingThisPensionYear);
            isCappeddrawdown = true;
        }

        //Adds totals for all tranches
        totalTrancheValues = totalTrancheValues + Convert.ToDecimal(tranche.Value == null ? 0 : tranche.Value);
    }
%>

<div class="form-group">
     <label class="col-sm-offset-1 col-sm-4 control-label">SIPP cash balance</label>
     <div class="col-sm-4">
          <input id="SIPPCashBalance_Textbox" name"SIPPCashBalance_Textbox" value=<%: Math.Round(client.Sipp.Cash, 2) %> disabled />
     </div>
</div>
<% if (isCappeddrawdown)
{%>
     <div class="form-group">
          <label class="col-sm-offset-1 col-sm-4 control-label">Remaining income</label>
          <div class="col-sm-4">
               <input id="RemainingIncome_Textbox" name="RemainingIncome_Textbox" value=<%: sum %> disabled />
          </div>
     </div>
<%} %>

I tried wrapping convert.todecimal around my 'sum' but it just will not work

When I use F12 it says my value is value="30.00" but on my webpage it displays only as 30

I have tried using it as a string and have tried string.Format("{0:C}", sum) and sum.toString() but my field just will not display the '.00'


Solution

  • Jeroen comment is correct.

    Switch <%: sum %> to <%: sum.ToString("£ 0.00") %> or even better: <%: string.Format("{0:C}", sum) %>, which works with locale, making your string format localized.

    Edit: The "C" refers to a Currency format, which inherits from the CultureInfo. That makes it easy to localize your application. Most .Net classes override the .ToString() method to supply many different formats (specially numbers).

    You should check'em out: https://msdn.microsoft.com/pt-br/library/fzeeb5cd(v=vs.110).aspx