Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Brian 77 posts 251 karma points
    Nov 03, 2014 @ 15:41
    Brian
    0

    Formatting Numeric Datatype as currency

    Hi all,

    Another (hopefully) basic operation here that is flumexing me thus far!

    I have an estate agent website and I am using Numeric as the property values, my issue is with formatting the value to include a , after the first 3 digits e.g. £500,000.

    Any ideas on how I can format the numeric figures please guys in the code below:

    <li>Capital Value: <span class="thin">£@Umbraco.Field("capitalValue")</span></li>

    Please note that this is going straight into the template as opposed to Partial Macro or XSLT.

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Nov 03, 2014 @ 19:28
    Dennis Aaen
    1

    Hi Brian,

    An example of how you could do it is by using this:

    @{
    if (CurrentPage.HasValue("capitalValue")){
    var price = CurrentPage.capitalValue.ToString();
    <li>Capital Value: <span class="thin">£@price.Substring(0,3),@price.Substring(3,3)</span></li>
    }
    }

    And this can be place directly into your template, but you could also create a partial view or a partial view macro. In this example I just check if the field on the current page has a value, if so, then I make to subtrings, the first one, takes the first 3 digits, and the last takes the last 3 digits. And to make this solution more robust, you could add a regular expression on the field where the user should type in the price.

    I have made a regular expression where it only allows 0-6 characters, the regular expression look like this:

    ^[0-9]{0,6}$

    Hope this could be a solution for you.

    /Dennis

  • Mike Chambers 636 posts 1253 karma points c-trib
    Nov 03, 2014 @ 20:39
  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Nov 03, 2014 @ 21:18
    Dennis Aaen
    101

    Hi Brian,

    Based on the post Mike has linked to I have developed a simpler solution than the first one I made.

    @{ 
        if (CurrentPage.HasValue("capitalValue")){
            int price = CurrentPage.capitalValue;
            string formattedPrice = price.ToString("#,##0");

            £@formattedPrice    
    }
    }

    Hope this helps,

    /Dennis

  • Brian 77 posts 251 karma points
    Nov 04, 2014 @ 08:01
    Brian
    0

    That's great, thanks guys.

    Mike, good to see you around chap, long time!! you should email me sometime [email protected]..

  • Arlan 58 posts 184 karma points
    Dec 24, 2015 @ 19:16
    Arlan
    0

    Using Dennis method

    @{if (CurrentPage.HasValue("reportPrice"))
                                    {
                                        string reportPriceString = (string)CurrentPage.reportPrice;
                                        int reportPriceInteger = reportPriceString.AsInt();
    
                                        string reportPriceFormatted = reportPriceInteger.ToString("#,##0");
    
                                        @reportPriceFormatted
    
                                    }
                                    }
    
  • gary 385 posts 916 karma points
    Dec 24, 2015 @ 19:26
    gary
    0

    Hi

    Surprisingly, this works

    @CurrentPage.ReportPrice.ToString("#,##0")
    

    Regards

    Gary

Please Sign in or register to post replies

Write your reply to:

Draft