Newsgroups: comp.lang.prolog
Path: cantaloupe.srv.cs.cmu.edu!das-news2.harvard.edu!news2.near.net!news.mathworks.com!gatech!swrinde!pipex!sunic!sunic.sunet.se!news.lth.se!news.lu.se!venus.ling.lu.se!user
From: Johan.Dahl@ling.lu.se (Johan Dahl)
Subject: Re: how can I converting a real to integer?
Message-ID: <Johan.Dahl-2504950038280001@venus.ling.lu.se>
Sender: news@nomina.lu.se (USENET News System)
Nntp-Posting-Host: venus.ling.lu.se
Organization: Lingvistics & Phonetics, Univ. of Lund, Sweden
References: <3ngrio$22n@csugrad.cs.vt.edu>
Date: Mon, 24 Apr 1995 22:38:28 GMT
Lines: 44

In article <3ngrio$22n@csugrad.cs.vt.edu>, btvan@csugrad.cs.vt.edu (Thinh
Ba Van) wrote:

> I'd like to print out a real without the trailing decimal.  Is there 
> anyway to do this?

Hi

This is very implementation dependent, but try some of these (R is the
real and N is the integer)


N is int(R).
N is integer(R).

N is round(R). %% Might do rounding to nearest integer (2.35 => 2, 2.56 => 3)

N is ceil(R). %% Rounds to integer above or equal to R

N is floor(R). %% Rounds to integer below.

N is trunc(R).

You can also try to use these as two argument predicates:

int(R, N) etc etc

If none of these works you can use this brutal approach :

real_to_int(R, N) :-
  name(R, RL),                %% Convert Real to a list of ascii codes
  append(NL, [46 | _], RL),   %% Bind NL to the codes before the decimal
point      
                              %% (. = ascii 46)
  name(N, NL).                %% Convert NL to integer N
real_to_int(R, R).            %% Case if there is no decimal point


Hopes this helps, Johan Dahl
-- 
Johan Dahl Johan.Dahl@ling.lu.se
Department of Lingvistics and Phonetics
University of Lund
Sweden
