How do I remove trailing zeros from a decimal number?

Certified Senior Developer

How do I remove trailing zeros from a decimal number which is in text format. For Ex. Input 12345.60891200 should return 12345.608912 as output.
We can't use decimal as input or output as it has its limitation that increments last number or puts number into exponential format. I want to keep number as it is except removing trailing zeros if any so that actual value does not change.

OriginalPostID-225901

OriginalPostID-225901

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Lead Developer
    I can't find anything in the text() function that will do this per se, though I could be wrong. The only solution I can think of will rely on looping, though someone else might know of something more lightweight. But this should work.

    1) make a rule consisting of this, which will remove the right-most character only if it's 0:
    rule!removeTrailingZeros_sub -
    if(
    and(
    or(
    ri!text[ri!index] = "0",
    ri!text[ri!index] = "."
    ),
    len(ri!text) = ri!index,
    find(".", ri!text) <> 0
    ),

    replace(
    ri!text,
    ri!index,
    1,
    ""
    ),
    ri!text
    )

    Then in the parent, enter the following:
    reduce(
    rule!removeTrailingZeros_sub(
    text: _,
    index: _
    ),
    ri!text,
    reverse(
    enumerate(len(ri!text))+1
    )
    )

    Basically this looping rule will step backwards through every character of the entered string and remove trailing zeroes - but only when they're after the decimal point. I tweaked it so that if there are ONLY zeros after the decimal, it also removes the decimal, though you could potentially tweak it to other behavior like leaving behind one or two zeros, etc.
Reply
  • 0
    Certified Lead Developer
    I can't find anything in the text() function that will do this per se, though I could be wrong. The only solution I can think of will rely on looping, though someone else might know of something more lightweight. But this should work.

    1) make a rule consisting of this, which will remove the right-most character only if it's 0:
    rule!removeTrailingZeros_sub -
    if(
    and(
    or(
    ri!text[ri!index] = "0",
    ri!text[ri!index] = "."
    ),
    len(ri!text) = ri!index,
    find(".", ri!text) <> 0
    ),

    replace(
    ri!text,
    ri!index,
    1,
    ""
    ),
    ri!text
    )

    Then in the parent, enter the following:
    reduce(
    rule!removeTrailingZeros_sub(
    text: _,
    index: _
    ),
    ri!text,
    reverse(
    enumerate(len(ri!text))+1
    )
    )

    Basically this looping rule will step backwards through every character of the entered string and remove trailing zeroes - but only when they're after the decimal point. I tweaked it so that if there are ONLY zeros after the decimal, it also removes the decimal, though you could potentially tweak it to other behavior like leaving behind one or two zeros, etc.
Children
No Data