Forum

    • Register
    • Login
    • Search
    • Categories

    Lose the .0 in label / value

    General Questions
    4
    13
    209
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • DanH
      DanH last edited by

      I've asked this before ages ago but cannot for the life of me find it. If I display a value of a knob in a label I get (since develop) the decimal point included, even if the value is an integer, eg 1.0. 2.0 where I just want 1, 2 etc.

      There's an simple line of code that's need to be in the callback if I remember correctly...

      Matt_SF ustk d.healey 3 Replies Last reply Reply Quote 0
      • Matt_SF
        Matt_SF @DanH last edited by Matt_SF

        @DanH

        Engine.doubleToString(numDecimal, value)
        

        If I remember correctly.

        Develop branch - Build : may 14
        Win10 / Monterrey & Xcode 13

        1 Reply Last reply Reply Quote 0
        • ustk
          ustk @DanH last edited by

          @DanH @Matt_SF doubleToString parameters are the other way round Engine.doubleToString(double value, int digits)
          In this case a simple Math.round(value) will remove the last digit the other function can't

          Tired to press F5 in the forum...
          Studio427 Audio - Audio Instruments & FX Plugins for music production. Website - Facebook

          DanH Matt_SF 2 Replies Last reply Reply Quote 0
          • DanH
            DanH @ustk last edited by

            @ustk @Matt_SF thanks both, I've written it like this in the callback, is there an easier way?

            local x = Math.round(value);
            LabelValue.set("text", x);
            
            ustk 1 Reply Last reply Reply Quote 0
            • ustk
              ustk @DanH last edited by

              @DanH you don't need to create a variable for this as it is simple to understand, you can directly put the round function inside the label set

              Tired to press F5 in the forum...
              Studio427 Audio - Audio Instruments & FX Plugins for music production. Website - Facebook

              1 Reply Last reply Reply Quote 0
              • d.healey
                d.healey @DanH last edited by

                @DanH said in Lose the .0 in label / value:

                even if the value is an integer, eg 1.0. 2.0

                Those aren't integers 😉

                You could also use parseInt()

                Libre Wave - Freedom respecting instruments and effects
                My Patreon - HISE tutorials
                YouTube Channel - Public HISE tutorials

                DanH ustk 2 Replies Last reply Reply Quote 0
                • DanH
                  DanH @d.healey last edited by

                  @d-healey thanks, should have used a full stop rather than a comma 😆

                  1 Reply Last reply Reply Quote 0
                  • Matt_SF
                    Matt_SF @ustk last edited by

                    @ustk said in Lose the .0 in label / value:

                    @DanH @Matt_SF doubleToString parameters are the other way round Engine.doubleToString(double value, int digits)
                    In this case a simple Math.round(value) will remove the last digit the other function can't

                    My mistake, sorry 👍

                    Develop branch - Build : may 14
                    Win10 / Monterrey & Xcode 13

                    1 Reply Last reply Reply Quote 0
                    • ustk
                      ustk @d.healey last edited by ustk

                      @d-healey said in Lose the .0 in label / value:

                      You could also use parseInt()

                      I don't like the parseInt function, because, in some (rare) cases, it treats a leading zero as an octal number. I agree this is rarely the case (it happens for instance when dealing with dates).

                      example:

                      Console.print(parseInt("0500"));	// => 320
                      Console.print(Math.round("0500"));	// => 500
                      

                      Though in this case, it is not a problem to use it, I just personally feel like wanting to stay away from it 🙂

                      EDIT:
                      I just discovered that when not using a string, both are converting octal to base10!
                      (this makes sense since they are written in an octal fashion...)

                      Console.print(parseInt(0500));		// => 320
                      Console.print(Math.round(0500));	// => 320
                      

                      But I don't see a scenario where this could happen anyway...

                      Tired to press F5 in the forum...
                      Studio427 Audio - Audio Instruments & FX Plugins for music production. Website - Facebook

                      d.healey 1 Reply Last reply Reply Quote 0
                      • d.healey
                        d.healey @ustk last edited by

                        @ustk Is there an efficiency difference between the two functions?

                        Libre Wave - Freedom respecting instruments and effects
                        My Patreon - HISE tutorials
                        YouTube Channel - Public HISE tutorials

                        ustk 2 Replies Last reply Reply Quote 0
                        • ustk
                          ustk @d.healey last edited by

                          @d-healey That is what I'm wondering... Making a benchmark to see...

                          Tired to press F5 in the forum...
                          Studio427 Audio - Audio Instruments & FX Plugins for music production. Website - Facebook

                          1 Reply Last reply Reply Quote 0
                          • ustk
                            ustk @d.healey last edited by

                            @d-healey Another reason to stick with Math.round() !

                            reg a = 0;
                            
                            inline function round(dec)
                            {
                            	return Math.round(dec);
                            }
                            
                            Console.startBenchmark();
                            
                            for (i=0;i<100000;i+=0.3333)
                            {
                            	a = parseInt(i);	// 130-135ms
                            	a = Math.round(i);	// 50-53ms
                            	a = round(i);		// 68-70ms
                            }
                            	
                            Console.stopBenchmark();
                            

                            Tired to press F5 in the forum...
                            Studio427 Audio - Audio Instruments & FX Plugins for music production. Website - Facebook

                            d.healey 1 Reply Last reply Reply Quote 1
                            • d.healey
                              d.healey @ustk last edited by

                              @ustk Good test, I get similar results on my system. So Math.round() is the way to go!

                              Libre Wave - Freedom respecting instruments and effects
                              My Patreon - HISE tutorials
                              YouTube Channel - Public HISE tutorials

                              1 Reply Last reply Reply Quote 0
                              • First post
                                Last post

                              6
                              Online

                              740
                              Users

                              5.4k
                              Topics

                              50.4k
                              Posts