Skip to main content

Search

Formatting and Adjusting Numbers

Comments

3 comments

  • Adis Jugo

    Hi Michael,

     

    yes that is actually pretty easy. 

     

    In the first case, change the expression type to the "Assignment" and use the following expression:

    [[For_Vessel.Vessel_Initials]] + "-" + ('00000' + [[ID]]).slice(-5)

     

    Note that there are 5 zeros in the "prepended" zeros ("00000") and .slice() block also needs to be set to -5.

     

    You can do the same with "1" part:

     

    [[For_Vessel.Vessel_Initials]] + "-" + ('01000' + [[ID]]).slice(-5)

     

    This will result with HA-01012

     

    Best, Adis

    0
  • Michael Cummins

    Thanks for the reply!  This seems to be string manipulation though, yes?  So the numbers get a little messed up when you go from [[ID]] = 1 to 10 to 100, if I understand correctly?

     

    [[Vessel.Vessel_Initials]] + "-" + ('04000' +  1).slice(-5)

    Evaluation succeeded!Value: "HA-40001"

     

    [[Vessel.Vessel_Initials]] + "-" + ('04000' +  10).slice(-5)

    Evaluation succeeded!Value: "HA-00010"

     

    [[Vessel.Vessel_Initials]] + "-" + ('04000' +  100).slice(-5)

    Evaluation succeeded!Value: "HA-00100"

     

    Is there a way that I can format a number?

    0
  • Christof Nussbaumer

    Hi Michael Cummins

     

    Handle the static part as string and the dynamic leading zeros seperately.

    Check this out:

    function pad (str, max) {
         str = str.toString(); 
         return str.length < max ? pad("0" + str, max) : str;
    }
    return "HA-04"+pad([[ID]], 3);

    Results (3, 11, 999): HA-04003, HA-04011, HA-04999

    0

Please sign in to leave a comment.