Sunday, January 21, 2018

You are not logged in.

## #1 2003-07-08 08:38:43 am

Member
Registered: 2003-07-01
Posts: 4

### 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.

Bob

Offline

## #2 2003-07-08 08:53:46 am

Rob
Member
From:: Ohio, USA
Registered: 2002-11-20
Posts: 1886

### Re: need to convert a real number to integer

I think this might work.

set x to 3.14
set y to x div 1

-- Rob

Offline

## #3 2003-07-08 01:46:26 pm

jonn8
Member
From:: New York
Registered: 2003-05-01
Posts: 1766
Website

### Re: need to convert a real number to integer

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

set the_num to 3.56
set the_num to round of the_num rounding down

--this script was automatically tagged for
--color coded syntax by Script to Markup Code
--written by Jonathan Nathan

Jon

Offline

## #4 2003-07-08 04:02:58 pm

Camelot
Member
From:: San Jose, CA
Registered: 2002-12-15
Posts: 629

### Re: need to convert a real number to integer

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

Unless the number is negative.

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

Offline

## #5 2010-04-29 08:53:50 am

Jeffkr
Member
Registered: 2005-11-05
Posts: 458

### Re: need to convert a real number to integer

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".

#### Applescript:

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

Offline

## #6 2010-04-29 10:54:29 am

Jerome
Member
From:: Ohio
Registered: 2003-06-14
Posts: 709
Website

### Re: need to convert a real number to integer

Another way might be:

#### Applescript:

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.

Last edited by Jerome (2010-04-29 10:55:02 am)

Offline

## #7 2010-04-29 11:36:25 am

blend3
Member
From:: UK
Registered: 2006-03-28
Posts: 464

### Re: need to convert a real number to integer

and another!!

#### Applescript:

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!!

Last edited by blend3 (2010-04-29 11:38:41 am)

I can resist everything in life except temptation.
(Oscar Wilde)

Offline

## #8 2010-04-29 11:47:13 am

Jeffkr
Member
Registered: 2005-11-05
Posts: 458

### Re: need to convert a real number to integer

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?

#### Applescript:

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

Offline

## #9 2010-04-29 12:27:20 pm

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4488

### Re: need to convert a real number to integer

Hi.

#### Applescript:

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.

NG

Offline

## #10 2010-04-29 01:13:49 pm

Jeffkr
Member
Registered: 2005-11-05
Posts: 458

### Re: need to convert a real number to integer

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

Offline

## #11 2010-04-29 05:41:27 pm

Caleb531
Member
From:: Inside my Mac
Registered: 2009-11-27
Posts: 46
Website

### Re: need to convert a real number to integer

Couldn't this work?

#### Applescript:

set num to "3.14"
get num as integer

-Caleb

Offline

## #12 2010-04-30 12:47:53 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4488

### Re: need to convert a real number to 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.

#### Applescript:

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

NG

Offline

## #13 2010-05-04 04:02:47 am

McUsr
Member
From:: Southern Norway
Registered: 2010-04-07
Posts: 1776

### Re: need to convert a real number to 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

Mercurial vcs is a joy to use for scripting.

Filed under: numbers, decimal

Offline