0

I want to show 12345.678 as 12,345 in my .cshtml file using Razor. Is there any INLINE solution?

When I use:

<td>  @(myValue != null ? myValue.ToString().Substring(myValue.ToString().IndexOf(".") + 1).ToString("#,##0") : '-')</td>

The error message is:

The best overloaded method match for 'string.ToString(System.IFormatProvider)' has some invalid arguments

And for:

<td>@(myValue != null ? myValue.ToString().Format("{0:0}", myValue).ToString("#,##0") : '-')</td>

The error message is:

Member 'string.Format(string, object)' cannot be accessed with an instance reference;

trashr0x
  • 6,457
  • 2
  • 29
  • 39
Elnaz
  • 2,854
  • 3
  • 29
  • 41

3 Answers3

2

Based on the comments in the OP, it looks like you're passing a double to the view via the ViewBag, converting it to long, and then attempting to format it inline?

Essentially:

  • Converting 12345.678D to long, thus becoming 12345
  • Formatting 12345 to 12,345

This should work:

<td>@string.Format("{0:n0}", Convert.ToInt64(ViewBag.MyVal))</td>

Bear in mind that as already pointed out by @David, you're better off passing a viewmodel back to the view as opposed to ViewBag, with myValue being a property of the viewmodel itself. You can see how to implement viewmodels in my answer here.

That being said, at the very least, I would abstract the logic away by creating my own class (let's call it MyHtmlHelper) which extends HtmlHelper:

namespace System.Web.Mvc.Html
{
    public static class MyHtmlHelper
    {
        public static string DoubleToFormattedLong(this HtmlHelper html, double myValue)
        {
            var longValue = Convert.ToInt64(myValue);
            return string.Format("{0:n0}", longValue);
        }
    }
}

Usage:

<td>@Html.DoubleToFormattedLong((double)ViewBag.MyVal)</td>

I'm not performing any validation whatsoever in my examples, but validation should ideally also be in place.

trashr0x
  • 6,457
  • 2
  • 29
  • 39
1

For the second one, just as the error says, you are trying to invoke .Format() on an instance of a string, but .Format() is a static method. So instead of this:

myValue.ToString().Format("{0:0}", myValue).ToString("#,##0")

You'd do something like this:

string.Format("{0:0}", myValue).ToString("#,##0")

Note that myValue is passed as an argument to the static method, you don't call the method on the object itself.

As an aside, in general when you're working in any sort of MVC (or MVVM, etc.) pattern and finding yourself trying to do even remotely complex inline code in a view, what you're really looking for is to add a calculated property to your model and then just bind to that property. The logic belongs in the view model, not the view itself.

David
  • 208,112
  • 36
  • 198
  • 279
0

you can extend function for the integer or other numeric data type like this

 namespace Utility
 {
      public static class Global
      {
           public static string DecimalSepeartor(this int s)
           {
                NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;
                return (s.ToString("N", nfi));
           }
      }
 }

then you can use this extended function as bellow

@using Utility
//
//
//

<div> 
     @Model.Amount.DecimalSepeartor() 
</div>
Hamid Jolany
  • 800
  • 7
  • 11