List Info

Thread: The Calculation




The Calculation
country flaguser name
United States
2007-03-22 06:15:25

Hello world how are you.
Im doing calculation in my delphi program for example im calculating 7751.9 / 2991 and geting result 2,57970578401872 how can I cut after 2.5 that its calculate like this 7751.9/2991 = 2.5 and not 2,57970578401872

thanks a lot
Arsen Khachatryan
-------------------------------
Dating with the russian girls
http://khaarsen.ueuo.com/love.html

__________________________________________________________
We won't tell. Get more on shows you hate to love
(and love to hate): Yahoo! TV's Guilty Pleasures list.
http://tv.yahoo.com/collections/265

[Non-text portions of this message have been removed]

__._,_.___
.

__,_._,___
Re: The Calculation
country flaguser name
United Kingdom
2007-03-22 06:37:46

Hello Arsen

You could just use the format statement but in your example it would
give the answer 2.6. To get 2.5 you need to use a truncation. This is
easiest done at the integer level, so multiply up, truncate then divide
back down.

Here is an untested thought:

var
i : integer;
a : single;
s : string;
begin
a := 7751.9 / 2991;
a := 10 * a;
i := trunc(a);
a := i / 10;
s := format('%f5.1,[a]); // s := '2.5'
end;

Does it work? If the answer can be negative, check that you get
truncation to the correct side. If not, use the floor function instead
of trunc.

Bobby Clarke

Arsen Khachatryan wrote:
>
> Hello world how are you.
>; Im doing calculation in my delphi program for example im calculating
> 7751.9 / 2991 and geting result 2,57970578401872 how can I cut after
> 2.5 that its calculate like this 7751.9/2991 = 2.5 and not
> 2,57970578401872
>
> thanks a lot
> Arsen Khachatryan
> -------------------------------
> Dating with the russian girls
&gt; http://khaarsen.ueuo.com/love.html <http://khaarsen.ueuo.com/love.html>
>
>; __________________________________________________________
> We won't tell. Get more on shows you hate to love
>; (and love to hate): Yahoo! TV's Guilty Pleasures list.
&gt; http://tv.yahoo.com/collections/265 <http://tv.yahoo.com/collections/265>
>
&gt; [Non-text portions of this message have been removed]
>
>;
> ----------------------------------------------------------
>
&gt; No virus found in this incoming message.
> Checked by AVG Free Edition.
> Version: 7.5.446 / Virus Database: 268.18.13/726 - Release Date: 18/03/2007 15:34
&gt;

[Non-text portions of this message have been removed]

__._,_.___
.

__,_._,___
Re: The Calculation
country flaguser name
Greece
2007-03-22 12:51:21

Also you can try this

function DoubleStripDigits(aouble;dig:integer):String;
begin
// because format rounds, add 2 more digits then wanted
result := format('%.*f',[dig&#43;2, a]);
// and then cut them
result := copy(result,1,length(result)-2);
end;

calling as
DoubleStripDigits(a,2)
to have as string
or
StrToFloatDef(DoubleStripDigits(a,2),0)
to have it as double

*********** REPLY SEPARATOR ***********
>Arsen Khachatryan wrote:
&gt;
> Hello world how are you.
>; Im doing calculation in my delphi program for example im calculating
> 7751.9 / 2991 and geting result 2,57970578401872 how can I cut after
> 2.5 that its calculate like this 7751.9/2991 = 2.5 and not
> 2,57970578401872
&gt;
**************************************

__._,_.___
.

__,_._,___
Re: The Calculation
country flaguser name
United Kingdom
1969-12-31 18:00:00

Hi Antonis

The code below will give different results from the others. Consider
starting with 2.5996. Your solution will round up to 2.6. Other code
would return 2.5. It really depends what result you actually want for
any input.

Bobby Clarke

Antonis Tsourinakis wrote:
&gt;
> Also you can try this
>;
> function DoubleStripDigits(aouble;dig:integer):String;
&gt; begin
&gt; // because format rounds, add 2 more digits then wanted
&gt; result := format('%.*f',[dig&#43;2, a]);
>; // and then cut them
>; result := copy(result,1,length(result)-2);
> end;
>;
> calling as
> DoubleStripDigits(a,2)
> to have as string
&gt; or
> StrToFloatDef(DoubleStripDigits(a,2),0)
> to have it as double
&gt;
> *********** REPLY SEPARATOR ***********
> >Arsen Khachatryan wrote:
&gt; >
>; > Hello world how are you.
>; > Im doing calculation in my delphi program for example im calculating
> > 7751.9 / 2991 and geting result 2,57970578401872 how can I cut after
&gt; > 2.5 that its calculate like this 7751.9/2991 = 2.5 and not
> > 2,57970578401872
&gt; >
>; **************************************
>
>;
> ----------------------------------------------------------
>
&gt; No virus found in this incoming message.
> Checked by AVG Free Edition.
> Version: 7.5.446 / Virus Database: 268.18.13/726 - Release Date: 18/03/2007 15:34
&gt;

[Non-text portions of this message have been removed]

__._,_.___
.

__,_._,___
Re The Calculation
country flaguser name
Greece
2007-03-23 07:46:38

You are right
I use this piece of code when I want two 9 digits to rounded.
Changing a little bit the function, I think you get the wanted results
(Concole application in D7)
/*************************************/
program Project1;
{$APPTYPE CONSOLE}
uses
SysUtils;

function DoubleStripDigits(aouble;dig:integer):String;
var p: Integer;
begin
result := floatToStr(a);
p := pos(DecimalSeparator, result);
if p>0 then
result := copy(result, 1, p + dig);
end;
var
i : integer;
a : double;
s : string;
begin
// a := 7751.9 / 2991.546546;
a := 2.599999999996;
writeln('a->',a:10:20);
for i := 9 downto 0 do
begin
s := DoubleStripDigits(a, i);
writeln(i, '->', s);
end;
readln;
end.
*****************************/
you get
2.59999999999600018000 <- forcing to write with too many digits look an error
9->2,599999999
8-&gt;2,59999999
7->2,5999999
6->2,599999
5->;2,59999
4->2,5999
3->2,599
2-&gt;2,59
1->2,5
0->2,

*********** REPLY SEPARATOR ***********

On 23/03/2007 at 11:41 Bobby Clarke wrote:

>Hi Antonis
>
>The code below will give different results from the others. Consider
>starting with 2.5996. Your solution will round up to 2.6. Other code
>would return 2.5. It really depends what result you actually want for
>any input.
&gt;
>Bobby Clarke
&gt;
>
>
&gt;Antonis Tsourinakis wrote:
&gt;>
&gt;> Also you can try this
>;>
>;> function DoubleStripDigits(aouble;dig:integer):String;
&gt;> begin
&gt;> // because format rounds, add 2 more digits then wanted
&gt;> result := format('%.*f',[dig&#43;2, a]);
>;> // and then cut them
>;> result := copy(result,1,length(result)-2);
>&gt; end;
>;>
>;> calling as
>&gt; DoubleStripDigits(a,2)
>&gt; to have as string
&gt;> or
>&gt; StrToFloatDef(DoubleStripDigits(a,2),0)
>> to have it as double
&gt;>
&gt;> *********** REPLY SEPARATOR ***********
>>; >Arsen Khachatryan wrote:
&gt;> >
>;> > Hello world how are you.
>;> > Im doing calculation in my delphi program for example im calculating
>>; > 7751.9 / 2991 and geting result 2,57970578401872 how can I cut after
&gt;> > 2.5 that its calculate like this 7751.9/2991 = 2.5 and not
>> > 2,57970578401872
&gt;> >
>;> **************************************
>>
>>
>>; ----------------------------------------------------------
>>
>> No virus found in this incoming message.
>> Checked by AVG Free Edition.
>> Version: 7.5.446 / Virus Database: 268.18.13/726 - Release Date:
&gt;18/03/2007 15:34
&gt;>
>
>
>[Non-text portions of this message have been removed]
>
>;
>
>-----------------------------------------------------
&gt;Home page: http://groups.yahoo.com/group/delphi-en/
>To unsubscribe: delphi-en-unsubscribe%40yahoogroups.com">delphi-en-unsubscribeyahoogroups.com
>Yahoo! Groups Links
&gt;
>
>

__._,_.___
.

__,_._,___
[1-5]

about | contact  Other archives ( Real Estate discussion Medical topics )