Copied to clipboard

Flag this post as spam?

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


  • Claushingebjerg 886 posts 2415 karma points
    Apr 07, 2017 @ 07:08
    Claushingebjerg
    0

    string to float in razor

    I need to do some simple calculation in my view, from numbers input to textfields in Umbraco. An input field is a "string" by default, as i understand. So i need to convert it to a calculatable format.

    Converting the string to int works fine, but removes the decimals, if there are any casuing miscalculaton.

    Converting to float works fine if the number has decimal, but fails if the number is actually an integer...

    How do i bulletproof the conversion so numbers like 12 or 12.3 or 12.30 or 00 or 0.0 all work in the calculation?

  • Damiaan 438 posts 1290 karma points MVP 3x c-trib
    Apr 07, 2017 @ 08:17
    Damiaan
    0

    It should "work" easily. Can you provide some code which is not working?

  • Claushingebjerg 886 posts 2415 karma points
    Apr 07, 2017 @ 12:56
    Claushingebjerg
    0

    It did, the error was elsewhere, but no less strange. I needed to calculate this:

    (((350 * antalTimer) + 2500) * 0.75) + befordring
    

    But for some reason i got errors when trying to do it in one go, so i ended up with:

    var calc = 350*timer+2500;
    var calc2 = (calc/100)*75;
    var refusion= calc2+befordring;
    

    which works :)

  • Damiaan 438 posts 1290 karma points MVP 3x c-trib
    Apr 07, 2017 @ 13:23
    Damiaan
    1

    So... your issue has magically solved by splitting into lines? :-)

    If you need decimals, you multiply with e.d. 2500m which is the same as (decimal)2500. I something alike would have solved your issue...

  • Brendan Rice 372 posts 608 karma points
    Apr 07, 2017 @ 13:50
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies