Board index » delphi » Integer value of a Double

Integer value of a Double


2005-10-04 03:47:05 AM
delphi37
Hi all
Silly question, but need this asap..,
I need to get the integer value of a double precision field in a stored
procedure.
Sure, i can do a Cast .. as Integer, or just assign the value to a Integer
variable, or use F_Truncate, or maybe even ABS function.
But all these functions will round the integer, if i for instance have 11,20
and do a cast I will have 11, if i have 11.55 and do a cast I will have 12.
That's the problem I am facing, i need to get the first part without being
round, no matter what the decimal value is.
I'm considering using substring functions but that seems so overdoing
things.
Any recommendations please?, i need to solve this very soon
I should have payed more attention during math class :p
Franky
 
 

Re:Integer value of a Double

Franky Brandt writes:
Quote
Hi all

Silly question, but need this asap..,

I need to get the integer value of a double precision field in a stored
procedure.
Sure, i can do a Cast .. as Integer, or just assign the value to a Integer
variable, or use F_Truncate, or maybe even ABS function.
But all these functions will round the integer, if i for instance have 11,20
and do a cast I will have 11, if i have 11.55 and do a cast I will have 12.
That's the problem I am facing, i need to get the first part without being
round, no matter what the decimal value is.
I'm considering using substring functions but that seems so overdoing
things.

Any recommendations please?, i need to solve this very soon
I should have payed more attention during math class :p

If F_Truncate rounds, try to subtract 0.5 from the value before
converting (if the value isn't less than zero, in which case ...)
Subtraction (and addition) with floating point is prone to lose
precision (it that is the right word), but if the numbers aren't too
different in magnitude you should be ok. IIRC.
--
Aage J.
 

Re:Integer value of a Double

Franky Brandt writes:
Quote
Hi all

Silly question, but need this asap..,

I need to get the integer value of a double precision field in a
stored procedure. Sure, i can do a Cast .. as Integer, or just
assign the value to a Integer variable, or use F_Truncate, or maybe
even ABS function. But all these functions will round the integer,
if i for instance have 11,20 and do a cast I will have 11,
Not too sure what you are looking for. I don't understand why a Trunc
of the value won't give you what you want.
11,20 trun = 11
11,99 turn = 11
Now if you want decimals places then that is a different story and you
can multiply before you truncate.
--
Rob Schieck
TeamB
 

Re:Integer value of a Double

Thanks for your replies Robert & Aage.
I was using the F_Truncate from FreeUdfLib function which does not work like
the examples u provided
If i use that function and do a F_Truncate of 11.99 in a integer I would get 12
as result.
So maybe it is the function, although i solved it in the meantime I am still
curious what Trun function u used Robert
Thank you,
Franky
<Robert Schieck (TeamB)>schreef in bericht
Quote
Franky Brandt writes:

>Hi all
>
>Silly question, but need this asap..,
>
>I need to get the integer value of a double precision field in a
>stored procedure. Sure, i can do a Cast .. as Integer, or just
>assign the value to a Integer variable, or use F_Truncate, or maybe
>even ABS function. But all these functions will round the integer,
>if i for instance have 11,20 and do a cast I will have 11,

Not too sure what you are looking for. I don't understand why a Trunc
of the value won't give you what you want.

11,20 trun = 11
11,99 turn = 11

Now if you want decimals places then that is a different story and you
can multiply before you truncate.

--
Rob Schieck
TeamB
 

Re:Integer value of a Double

I wrote and use to sell a UDF library called UDFlib....
It had a truncate function in it and it truncated the value not rounded
it. There was also round, ceiling, floor etc.
You could get the source for FreeUdfLib and fix it yourself.
hth
Rob
Franky Brandt writes:
Quote
Thanks for your replies Robert & Aage.

I was using the F_Truncate from FreeUdfLib function which does not
work like the examples u provided If i use that function and do a
F_Truncate of 11.99 in a integer I would get 12 as result. So maybe it's
the function, although i solved it in the meantime I am still curious
what Trun function u used Robert

Thank you,

Franky
--
Rob Schieck
TeamB