need to convert a real number to integer

How to I convert a real number to an integer?

lets say I have a nuber 3.14, and I want to make it 3.

I do not want to round (because 3.56) would become 4.

Basically I want to ignore anything to the right of the decimal.

Thanks in advance

Bob

I think this might work.

set x to 3.14
set y to x div 1

– Rob

You can also use the round command with the option to round down:

Jon

Unless the number is negative.

Rounding down -3.14 returns -4 which isn’t the same thing as -3.14 div 1

What if you only want to round any real number that ends as .0.
I have an input dialog box that I would like to retain as real if it is entered with decimal value. But if a digit such as “5” is entered, I don’t want it to yield “5.0”.

tell application "Adobe InDesign CS4"
	set myLabelWidth to 50
	set myDialog to make dialog with properties {name:"Make Swatch"}
	tell myDialog
		tell (make dialog column)
			tell (make dialog row)
				tell (make dialog column)
					make static text with properties {static label:"C  ", min width:myLabelWidth}
					make static text with properties {static label:"M  ", min width:myLabelWidth}
					make static text with properties {static label:"Y  ", min width:myLabelWidth}
					make static text with properties {static label:"K  ", min width:myLabelWidth}
				end tell
				tell (make dialog column)
					set myCField to make real editbox with properties {edit value:0}
					set myMField to make real editbox with properties {edit value:0}
					set myYField to make real editbox with properties {edit value:0}
					set myKField to make real editbox with properties {edit value:0}
				end tell
			end tell
		end tell
	end tell
	set myResult to show myDialog
	set myC to edit value of myCField
	set myM to edit value of myMField
	set myY to edit value of myYField
	set myK to edit value of myKField
end tell

Another way might be:

set oldDelim to AppleScript's text item delimiters
set x to -4.56

set AppleScript's text item delimiters to "."
set y to (first text item of (x as string)) as integer
set AppleScript's text item delimiters to oldDelim
return y

Should work for positive and negative numbers.

and another!!

set the_value to 3.14

if (the_value as string) contains "." then set new_value to text 1 thru ((offset of "." in (the_value as string)) - 1) of (the_value as string) as integer

return new_value

Personally I think Rob’s is the simplest and most effective!!

Thanks Jerome, but I don’t think I clarified my request the way I should have.

I am only trying to parse out a literal .0, and any other decimal value I would want to retain. For example:

5.0 ----> 5
5.5 ----> 5.5
6.26 ----> 6.26
7.0 ----> 7

I am using this type of premiss, which works, but it seems rather long for a seemingly simple thing?

set myC to 5.5 as text
set x to items -1 thru -2 of myC as text
if x is equal to ".,0" then
	set myC to myC div 1
else
	set myC to myC
end if

set myM to 5.0 as text
set x to items -1 thru -2 of myM as text
if x is equal to ".,0" then
	set myM to myM div 1
else
	set myM to myM
end if

return myC & return & myM

Hi.

if (myC mod 1 is 0.0) then set myC to myC div 1

This will only work up to the maximum value of an AppleScript integer: +/- (2 ^ 29) - 1, I think.

That’s genius!!! Much Much Better than what I had.
Don’t worry, I will tell operators not to input CMYK values to the 29th power.

Thank you.
-Jeff

Couldn’t this work?

set num to "3.14"
get num as integer

Hi, Caleb.

That specifically would not work because Jeff wanted to leave numbers with anything other than 0 after the decimal point alone. However, ‘as integer’ would do instead of ‘div 1’ in my solution in post #9 and is probably better, since it makes the intention clearer and is possibly slightly faster.

if (myC mod 1 is 0.0) then set myC to (myC as integer)

I’ll post another neat conversion which I saw at the Applescript users list, since it belong here.

Giving credits to Stan who gave credits to Shane who gave credits to someone else…

4999.0*12.5 as meters as text ( Without the coercion to meters AS would return 6.24875E+4)

Gives you the numbers with two decimals. I tried it and I think most of the units will do at least before you coerce from a unit to another.

Thought this was relevant to the topic.

Best Regards

McUsr


We are drowning in information but starved for knowledge.
– John Naisbitt, Megatrends