Arithmetic x25 ➜ Check if decimal ➜ Round ➜ Put back in place?

Discussion in 'Mac Programming' started by Riku7, Jul 18, 2015.

  1. Riku7, Jul 18, 2015
    Last edited: Jul 18, 2015

    Riku7 macrumors member

    Joined:
    Feb 18, 2014
    #1
    Been trying to look through official and unofficial guides but the level of nesting in this code is a bit too much for a newbie like me; Have to admit that I can't figure out how to compile this one.

    I have this Applescript in the making:
    • Dialog box asks user for numeric value input ➜ OK
    • Based on given value, 25 different arithmetic operations are made and displayed as a table.

    Some of these values come out with 12 decimal places which is way beyond necessary (2 decimal places max would do), and it also messes up the layout of the table because dialog boxes can only have so much width. How do I round and truncate results before displaying, if they have the risk of turning out ugly? I found guides on rounding, but I'm not sure how to use it when a), not every value has decimals and b) the values and to-be-ugly numbers aren't known until the function gets its numeric input from the user.

    I tried to define each arithmetic operation into their own variables which I'd then scatter into the table and I made the collection of calculated values into a list. Taking guesses here, should I make a loop that would check every value in the calculated value list for decimals, take the ugly values one by one for a truncation procedure and put them back, end loop when all looks nice, then display the tidied up table of values? Or does every calculating code have to have its own code for checking and truncating the number, forgetting about the whole list and loop thing?
    This was a bit too much nesting for me to find relevant guidance for. Whatever I've tried to write for truncating so far only gets me stuck. Any ideas, please?
     
  2. hokan macrumors member

    Joined:
    Mar 18, 2014
    Location:
    Sweden
    #2
    Most programing languages have some kind of print method/function that takes a format string and additional arguments (e.g. numbers, strings ...) to put into the format string when it prints/returns a string. The format string will typically support some way to specify how and with what precision to display numbers.

    This may look something like this:

    weight = 1.2345
    output_string = print_to_string("weight in %.2 kg", weight) // output_string = "weight in 1.23 kg"

    where '%' is a placeholder for the weight parameter and '.2' specifics the precision after the '.'
     
  3. cqexbesd macrumors regular

    Joined:
    Jun 4, 2009
    #3
    I'm no Applescripter but have you tried:

    Code:
    set n to (round (n * 100)) / 100
    
    to round the variable n to 2 decimal places?

    It's ugly..but thats probably why I don't use Applescript.
     
  4. Mark FX, Jul 19, 2015
    Last edited: Jul 20, 2015

    Mark FX macrumors regular

    Mark FX

    Joined:
    Nov 18, 2011
    Location:
    West Sussex, UK
    #4
    I'm assuming your script is old school vanilla AppleScript, and not AppleScriptObjC.
    If that is the case, then something like the below handler will do the job.
    Code:
    set myDecimalNumber to 123.456789 as real
    
    set myDecimalNumberText to my roundToDecimalPlaces(myDecimalNumber, 2) as text
    
    on roundToDecimalPlaces(theDecimalNumber, theDecimalPlaces)
        set theMultiplyer to 10 ^ theDecimalPlaces as real
        set theDecimalNumber to theDecimalNumber * theMultiplyer as real
        round result
        return result / theMultiplyer as real
    end roundToDecimalPlaces
    
    display dialog myDecimalNumberText
    
    Regards Mark
     

Share This Page