doubleToString 0 digit...



  • Engine.doubleToString(value, 0) doesn't throw a 0 digit string, but 2 digits oscillating with 0 digit...

    I know I can use round but it's not practical in my situation. I have a function that needs a different number of digit depending on the value & unit to display. Some need 1, other 0, and other 2 digits Engine.doubleToString(value, object.digit)



  • Yes, passing 0 as second argument means "give me all digits that are available" (actually I am just forwarding this to a JUCE helper method).

    But you can easily write a version that's more suitable for you like this:

    namespace GregsEngine
    {
        inline function doubleToString(value, numDigits)
        {
            if(numDigits == 0)
                return "" + parseInt(value);
            else
                return Engine.doubleToString(value, numDigits);
        }
    }
    
    // client code
    
    Console.print(GregsEngine.doubleToString(2412.0125, 0));
    Console.print(GregsEngine.doubleToString(2412.0125, 2));
    


  • @Christoph-Hart Wow cool! Thanks 👍


Log in to reply
 

11
Online

502
Users

1.8k
Topics

13.6k
Posts