Copied to clipboard

Flag this post as spam?

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


  • Matthew 93 posts 153 karma points
    Sep 19, 2012 @ 02:47
    Matthew
    0

    Can I use AsDecimal() in Umbraco?

    Since Umbraco doesn't do decimal numbers, can I store them in text properties and use AsDecimal() to convert them? So far my experiments haven't succeded and I'm wondering if I've entirely missed the boat again. I've been assuming that if I find it on the ASP.NET site, I can use it here. Is that true? Or does Umbraco use a limited subset of it.

    http://www.asp.net/web-pages/tutorials/basics/2-introduction-to-asp-net-web-programming-using-the-razor-syntax

     

  • Funka! 398 posts 661 karma points
    Sep 19, 2012 @ 04:33
    Funka!
    0

    My feeling says, yes, you can store a value as text and convert it to decimal when you need. Have you tried the intrinsic decimal.Parse(str)/TryParse(str, out dec) methods? I'm not sure about any "AsDecimal" methods, but there is a Convert.ToDecimal(str) that might also do the same thing... Paste a code sample and let's see if we can help!

     

     

     

     

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Sep 19, 2012 @ 14:27
    Dan Diplo
    0

    If the property value is returned as a string then, yes, AsDecimal() should work. However, if your string can't be cast to a Decimal value then it will return 0.

    To determine the actual type your property is sored as try:

    @Model.MyProperty.GetType()

    To force the property to be a string before conversion you could try:

    @Model.MyProperty.ToString().AsDecimal()

     

  • Matthew 93 posts 153 karma points
    Sep 19, 2012 @ 20:03
    Matthew
    0

    Thanks much for the replies.  I'm still very weak on the fundamentals, so please pardon my ignorance.

    @Funka!, I haven't tried Parse, it looked over my head.  When I try Convert.ToString, I get the 'Error loading MacroEngine script'.

    @Dan, you're right, GetType returns 'System.Decimal' although it's a textstring property, which I haven't found a good explanation for, if Umbraco doesn't do decimal.  When I try ToString...AsDecimal, I also get the 'Error loading MacroEngine script'.

    The goal is to store these values on each page, then present an average, so if 9 pages have values of 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9 and 2 (to keep it simple), then I'm aiming at getting the sum of those (15.4), getting the count of pages with values (9) and dividing the sum by the count for the average (15.4 / 9 = 1.721111111111111), and probably want to round that down to 1.72 (pardon if I've goofed the math).

    I was just trying this to try and get started:

    @using umbraco.MacroEngines;
    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{
    <p>
    @Model.annBalMWE<br/>
    @Model.cumBalYr<br/>
    @Model.annBalMWE.GetType()<br/>
    @Model.annBalMWE.Convert.ToString()<br/>
    @*@Model.annBalMWE.Convert.ToDecimal()<br/>*@
    </p>
    }

    I was alternatively commenting out the last two lines for testing.

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Sep 20, 2012 @ 13:56
    Dan Diplo
    0

    If you want to convert a property to a string you just call ToString() - no need for the Convert part ie.

     

    @Model.annBalMWE.ToString()
    @Model.annBalMWE.ToString().AsDecimal()

    The other way to ensure your value is a decimal would be:

    @Convert.ToDecimal(Model.annBalMWE)

    However, this would throw an error if the value could not be converted (say it was empty).

     

  • Matthew 93 posts 153 karma points
    Sep 22, 2012 @ 06:40
    Matthew
    0

    Thanks Dan.  I had tried that, just forgot to paste that sample.  I've tried it every which way I can think of now and it seems like any time I put AsDecimal() in there anywhere, whether after ToString() or just by itself, it errors with 'Error loading MacroEngine script'.  Has anyone else ever used AsDecimal(), or would someone mind testing if it works in Umbraco?  FWIW, ToString() seems to work fine.  Maybe Umbraco really, really hates decimals???

  • Matthew 93 posts 153 karma points
    Sep 22, 2012 @ 08:03
    Matthew
    0

    Ran into this post, which suggests using 'decimal', maybe:

    decimal d = @Model.annBalMWE.ToString();

    Tried that and several variations, still get that 'Error loading MacorEngine script..'.

    Grrrr....

     

  • Matthew 93 posts 153 karma points
    Sep 22, 2012 @ 20:09
    Matthew
    0

    Could it be erroring because I should be @using something else?  I've never found an explanation of how to use those properly.

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Sep 24, 2012 @ 12:31
    Dan Diplo
    0

    Matthew, try adding following to top of scripts:

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @using umbraco.MacroEngines;

    Does that help?

  • Matthew 93 posts 153 karma points
    Sep 24, 2012 @ 21:40
    Matthew
    0

    No, but thanks Dan.  I had that much in there already, I was thinking maybe there was something else.  I've seen some scripts that have maybe 4 different @using lines at the top.  I haven't found anything that explains what the various options are for the @using lines and how/when to use them.  Seems like if the asp.net site says Razor can do it that it oughtta work....

Please Sign in or register to post replies

Write your reply to:

Draft