Copied to clipboard

Flag this post as spam?

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


  • Martin 278 posts 662 karma points
    Dec 10, 2014 @ 13:37
    Martin
    0

    Grid Layout - Integer to Text

    Hi all, I had posted in the 7.2 category, but i think this is more to do with my lack of Razor knowledge. Im playing around with the 7.2 grid layout and was wanting to change the numeric grid value to a word value. Niels kindly pointed me in the direction of function that did this, but being a designer type, its a bit beyond me.

    I have the function NumberWords, but im having trouble passing the area.grid value through it. I can use a numerical value within the helper below "@NumberWords(4)", but im stumped on how to pass the "@area.grid" value in its place.

    Any help would be grateful.

    Martin

    @helper renderRow(dynamic row, bool singleColumn)
    {
        <div @RenderElementAttributes(row)>
            @Umbraco.If(singleColumn, "<div class='container'>")
            <div class="row clearfix">
                @foreach (var area in row.areas)
                {
                    <div class="[email protected] column @NumberWords(4)">
                        <div @RenderElementAttributes(area)>
                            @foreach (var control in area.controls)
                            {
                                if (control != null && control.editor != null && control.editor.view != null)
                                {
                                    <text>@Html.Partial("grid/editors/base", (object)control)</text>
                                }
                            }
    
                        </div>
                    </div>}
            </div>
            @Umbraco.If(singleColumn, "</div>")
        </div>
    }
    

    Function

    @functions {
    public static string NumberWords(int number)
    {
        if (number == 0)
            return "zero";
    
        if (number < 0)
            return "minus " + NumberWords(Math.Abs(number));
    
        string words = "";
    
        if ((number / 1000000) > 0)
        {
            words += NumberWords(number / 1000000) + " million ";
            number %= 1000000;
        }
    
        if ((number / 1000) > 0)
        {
            words += NumberWords(number / 1000) + " thousand ";
            number %= 1000;
        }
    
        if ((number / 100) > 0)
        {
            words += NumberWords(number / 100) + " hundred ";
            number %= 100;
        }
    
        if (number > 0)
        {
            if (words != "")
                words += "and ";
    
            var unitsMap = new[] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
            var tensMap = new[] { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
    
            if (number < 20)
                words += unitsMap[number];
            else
            {
                words += tensMap[number / 10];
                if ((number % 10) > 0)
                    words += "-" + unitsMap[number % 10];
            }
        }
    
            return words;
        }
    }
    
  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Dec 15, 2014 @ 14:22
    Andy Butland
    0

    It's probably an issue around the use of dynamic in the grid Razor, with the function expecting an integer.

    Try this...

    @NumberWords((int)grid.area)

    Andy

Please Sign in or register to post replies

Write your reply to:

Draft