Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8856435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:21:22+00:00 2026-06-14T14:21:22+00:00

Our database stores values along with their associated format. Some number have 0 numbers

  • 0

Our database stores values along with their associated format. Some number have 0 numbers after the decimal, some 1, some are currency, etc.

I am trying to avoid doing the following in each instance the data needs to be displayed.

@foreach (var i in Model.CandidateHireGoalsAndActivity)
{
    if (i.DataTypeId == 3)
    { 
        <tr>
            <td>@i.HireGoalName</td>
            <td>@i.ActivityValue.ToString("C0")</td>
            <td>@i.GoalValue.ToString("C0")</td>
        </tr>
    }
    else if (i.DataTypeId == 2)
    { 
        <tr>
            <td>@i.HireGoalName</td>
            <td>@i.ActivityValue.ToString("F2")</td>
            <td>@i.GoalValue.ToString("F2")</td>
        </tr>
    }
    else if....(etc)
}

I am trying to write a basic extension method that receives a string and desired format and then my code could look more like this.

    @foreach (var i in Model.CandidateHireGoalsAndActivity)
    {
       <tr>
          <td>@i.HireGoalName</td>
          <td>@Html.FormatString(i.ActivityValue.ToString(), i.DataTypeId)</td>
          <td>@Html.FormatString(i.GoalValue.ToString(), i.DataTypeId)</td>
       </tr>
    }

For some reason, the string coming out of my helper is never formatted. Whatever unformatted number I pass in is passed back out. I am not sure what I am doing wrong or missing.

helper method:

public static MvcHtmlString FormatString(this HtmlHelper helper, string stringToFormat, int format)
        {
            if (format == 3)
            {
                return MvcHtmlString.Create(String.Format("{0:C0}", stringToFormat));      
            }
            return MvcHtmlString.Create(String.Format("{0:0}", stringToFormat));
        }

I’ve also tried returning a regular string instead of the MvcHtmlString as well as using the ToString() method, but neither helped.

Any help/suggestions are much appreciated.

Thanks,
-Tim

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:21:23+00:00Added an answer on June 14, 2026 at 2:21 pm

    Your error here is you are trying to format a string, not a number. String.Format() can format only if you pass a number. So you just simply need to change parameter from string to double:

    public static MvcHtmlString FormatString(this HtmlHelper helper, double numberToFormat, int format)
    

    And don’t convert numbers in the view:

    @Html.FormatString(i.ActivityValue, i.DataTypeId)
    

    If you have not only double values, but also other types (like float or decimal) you can use object instead of double:

    public static MvcHtmlString FormatString(this HtmlHelper helper, object numberToFormat, int format)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm porting some PHP to C++. Some of our database code stores time values
Our scenario: We have a main database that stores company-wide information. We have several
I have 100000 Ids to store into our DataBase. Id is in string format.each
Our database currently stores 2 values - the longitude and the latitude. E.g.: Longitude:
In our inventory database (SQL Server 2008 std edition) we have a table (called
In our database model we have a Beneficiary entity. A beneficiary can be a
I am using Oracle 10g Enterprise edition. A table in our Oracle database stores
We have an audit table in our database, and on update the old and
I'm using Entity Framework code first and pulling some data back from our database.
In our business logic we have to handle positive and negative Double.Infinity values as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.