Write results of calculations always with two decimal places - SOLVED

Hello!

I’m writing an AppleScript to get time values out of text documents in BBEdit. It takes a list of time ranges, totals each line in decimal hours, then adds a grand total.

My times are input in ranges like:

09:30-09:45
11:30-12:00
13:00-14:45
15:15-17:15

The script converts the hours and minutes to a single seconds value, then does the subtraction to get the amount of time indicated by the range. I get these times to a decimal hours result by dividing by 3600. An example looks like this:

set TimeTotalLineCurrent to (TicketTextTimeLineStop - TicketTextTimeLineStart) / 3600

I always want the number of hours to have two decimal places, even with a trailing zero, but when I add the totals to the lines I get:

09:30-09:45 0.25
11:30-12:00 0.5
13:00-14:45 1.75
15:15-17:15 2.0 4.5

You’ll notice I also total all the lines and add it to the last line like the “4.5” in the example above, and I’d like that to be two decimal as well, or 4.50.

What I want this to look like is:

09:30-09:45 0.25
11:30-12:00 0.50
13:00-14:45 1.75
15:15-17:15 2.00 4.50

the way the script turns:

09:30-09:45
into
09:30-09:45 0.25

is with

# Add total time to end of current line in decimal hours
set BBLineNumber to startLine of selection
select insertion point after last character of line BBLineNumber of vDocument
set selection to " " & TimeTotalLineCurrent as text

Is there a way to make the text of TimeTotalLineCurrent to always have two decimals?

I’m curious as to why in my sample above the 2.0 gets its zero, but the 0.5 does not get a trailing zero. I would kind of expect it to just print “2”.

I’m also wondering why the 0.25 is getting its leading 0. - but this is exactly how I want it so I’m not complaining.

Thanks for any help!

  • eric

Working with and manipulating numbers is an important and regular occurrence in scripting. Basic mathematic operations—such as addition, subtraction, multiplication, and division—are language-level features, but some other commonly performed operations require custom scripting.

Oh! this looks promising. But, um, newbie here. How would I use this? is the part between “on” and “end” defining a, um, function or something, and I just have to stash it somewhere in my script, then use:
its numberWithDecimals(, 2, 2)
anywhere in the script to convert myVariable?

Thanks!

  • eric

Also, this wasn’t adding zeros until I changed to:

return theResult as string (not real)

you can use pure AppleScript

set TimeTotalLineCurrent to 0.5 as text
return text 1 thru (2 + (offset of "." in TimeTotalLineCurrent)) of (TimeTotalLineCurrent & "0")

or

set TimeTotalLineCurrent to text 1 thru (2 + (offset of "." in TimeTotalLineCurrent)) of (TimeTotalLineCurrent & "0")
1 Like

I didn’t mention that all of this was happening inside a “tell BBEdit” block. Will pure AppleScript work in there?

It should. Try it and see. or you could wrap this part in a “tell me” block
i.e. (most likely won’t need the “tell me” block)

tell application "BBEdit"
	-- do BBEdit stuff here
	
	tell me
		set TimeTotalLineCurrent to text 1 thru (2 + (offset of "." in TimeTotalLineCurrent)) of (TimeTotalLineCurrent & "0")
	end tell
end tell

Oh! Hey, it worked. I had to set my variable to a string, in the line where it was calculated, but then worked like a charm!

set TimeTotalLineCurrent to (TicketTextTimeLineStop - TicketTextTimeLineStart) / 3600 as string
set TimeRunningTotal to (TimeRunningTotal + TimeTotalLineCurrent)
# Make sure TimeTotalLineCurrent has two decimals
set TimeTotalLineCurrent to texts 1 thru (2 + (offset of "." in TimeTotalLineCurrent)) of (TimeTotalLineCurrent & "0")

Thanks much!!!

Your script above doesn’t compile because you pluralized “text” as “texts”
So did I by mistake. I edited it

Also you should convert TimeTotalLineCurrent to text before you execute the last line

set TimeTotalLineCurrent to text 1 thru (2 + (offset of “.” in TimeTotalLineCurrent)) of (TimeTotalLineCurrent & “0”)

I’m writing and debugging in application “Script Debugger”, and it makes the change from “text” to “texts” when I save. It is working as “texts”.

Are you typing in a languages besides English?

nope, just plain old US user using all US settings

I just tried in Apple’s Script Editor. I type in:

set TicketTimeSummaryEndMin to text 1 thru (2 + (offset of "" in TicketTimeSummaryEndMin)) of (TicketTimeSummaryEndMin & "0")

Then I Save, and the line changes to:

set TicketTimeSummaryEndMin to texts 1 thru (2 + (offset of "" in TicketTimeSummaryEndMin)) of (TicketTimeSummaryEndMin & "0")

Mac OS Ventura 13.5

You have nothing between the quotes. It should be a period

oh, sorry, I pulled this from another part of the script just to illustrate how “file” gets changed to “files”. This is a different condition where the variable is an integer number of minutes, and I needed to assure that 30 didn’t get represented as “3” - there is no “.” to key off of. Come to think of it, I could probably fix that in the math.

But, yes, the line totals you helped me with does use “.”.

Everything is working just great!

Nice catch!

no tell needed. Thanks!

FWIW:

-- inside a BBEdit tell block, "text"  compiles as "texts":
tell application "BBEdit"
	set TicketTimeSummaryEndMin to texts 1 thru (2 + (offset of "" in TicketTimeSummaryEndMin)) of (TicketTimeSummaryEndMin & "0")
end tell
--outside the tell block, "text" compiles as "text"
set TicketTimeSummaryEndMin to text 1 thru (2 + (offset of "" in TicketTimeSummaryEndMin)) of (TicketTimeSummaryEndMin & "0")