Forum Discussion

David91's avatar
David91
Frequent Contributor
2 years ago

Round digits number hundereths

I need some advice.

how to round the destination position to ..

5,21 to 5,20
5,22 to 5,20
5,23 to 5,25
5,24 to 5,25
5,25 to 5,25
5,26 to 5,25
5,27 to 5,25
5,28 to 5,30
5,29 to 5,30

is there any method I can use to round this up? I don't know what to do ğŸ˜ž

7 Replies

  • Marsha_R's avatar
    Marsha_R
    Champion Level 3

    My syntax is not exact but it will be something like this

     

    mystring = aqVarToStr(mynumber)

    if (aqString.SubString(mystring,0,1 = 1) or (aqString.SubString(mystring,0,1 = 2) then

       mynewstring = aqString.Substring(mystring,0,3) + "0"

       mynewnumber = aqConvert.StrtoVar(mynewstring)

    if (aqString.SubString(mystring,0,1 >= 3) or (aqString.SubString(mystring,0,1 <=7) then

       mynewstring = aqString.Substring(mystring,0,3) + "5"

       mynewnumber = aqConvert.StrtoVar(mynewstring)

    if (aqString.SubString(mystring,0,1 = 8 ) or (aqString.SubString(mystring,0,1 = 9) then

       mynewnumber = round(mynumber)

     

      

    • David91's avatar
      David91
      Frequent Contributor
      function test42(numb)
      {
      var mystr, mynewstring, mynewnumber;

      mystr = aqConvert.VarToStr(numb);

      if (aqString.SubString(mystr, 0, 1 = 1) || aqString.SubString(mystr,0,1 = 2))
      {
      mynewsting = aqString.SubString(mystr,0,3) + "0";
      Log.Message(mynewsting)
      mynewnumber = aqConvert.VarToStr(mynewstring);
      Log.Message(mynewnumber)
      }

      else if (aqString.SubString(mystr, 0, 1 >= 3) || aqString.SubString(mystr,0,1 <= 7))
      {
      mynewsting = aqString.SubString(mystr,0,3) + "5";
      Log.Message(mynewsting)
      mynewnumber = aqConvert.VarToStr(mynewstring);
      Log.Message(mynewnumber)
      }

      else if (aqString.SubString(mystr, 0, 1 = 8) || aqString.SubString(mystr,0,1 = 9))
      {
      mynewnumber = Math.round(numb)
      Log.Message(mynewnumber)
      }
      }

      I've rewritten your script into executable form but it doesn't work... it says error with number in first if .. photo

       

       

      • Marsha_R's avatar
        Marsha_R
        Champion Level 3
        if (aqString.SubString(mystr, 0, 1 = 1) || aqString.SubString(mystr,0,1 = 2))

         should be

         

        if (aqString.SubString(mystr,0,1) = 1 || aqString.SubString(mystr,0,1) = 2)

         

        note the change in parentheses and do that in the other ifs

         

         

  • Marsha_R's avatar
    Marsha_R
    Champion Level 3

    try it without the elses, just put three separate if statements

    • David91's avatar
      David91
      Frequent Contributor

       

       

       

      Log here..

      I put it only on ifs and now when calling a method with a value e.g. 8,11 it is still rounded up to 8,15 and in addition the third IF is fulfilled which in my opinion should not pass at all. mynewnumber = aqConvert.VarToStr(mynewstring); - after the conversion the value (for IF 1 and 2) is not displayed in the log at all... I don't know why 😕

       

      Thank you