Copied to clipboard

Flag this post as spam?

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


  • Simeon Ostberg 123 posts 389 karma points
    Dec 07, 2022 @ 17:08
    Simeon Ostberg
    0

    Do decimal mathematics in Razor email template

    Hi everyone,

    I have a Razor email template, and I would like to do simple mathematics in this code, e.g. amount1 + amount2, or amount1 * amount2.

    I tried this:

    @{
        string am1 = (string)Model.DynamicFields.amount1;
        string am2 = (string)Model.DynamicFields.amount2;
        int a1 = am1.AsInt();
        int a2 = am2.AsInt();
        var sum = (a1+a2);
        var product = (a1*a2);
        var division = (a1/a2);
        <H1>@sum</H1>
        <H1>@product</H1>
        <H1>@division</H1>
    }
    

    This basically works, however only for integers. How would I do this with decimals?

    Best, Simeon

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Dec 07, 2022 @ 18:07
    Huw Reddick
    100
  • Simeon Ostberg 123 posts 389 karma points
    Dec 12, 2022 @ 14:27
    Simeon Ostberg
    0

    Hi Huw,

    Thank you! I got it working with

    @{
    
        string am1 = (string)Model.DynamicFields.amount1;
        string am2 = (string)Model.DynamicFields.amount2;
        var a1 = Convert.ToDecimal(am1);
        var a2 = Convert.ToDecimal(am2);
        var sum = (a1+a2);
        var product = (a1*a2);
        var division = (a1/a2);
        <H1>@sum</H1>
        <H1>@product</H1>
        <H1>@division</H1>
    }
    

    Best, Simeon

Please Sign in or register to post replies

Write your reply to:

Draft