Solved

Correctly convert Float values.

Posted on 2006-11-29
21
321 Views
Last Modified: 2010-04-04
Hi,

I have some float fields in a database example the yen 127.60  I need to convert this to be left with a round integer value of 128

Thanks
Andy
0
Comment
Question by:Asw
  • 8
  • 6
  • 5
  • +1
21 Comments
 
LVL 28

Expert Comment

by:TName
ID: 18040346
Hi, why not simply:
Round(SomeFloat);
or
Round(127.60);
0
 
LVL 28

Expert Comment

by:TName
ID: 18040452
0
 
LVL 1

Author Comment

by:Asw
ID: 18040647
Hi,

I am calculating a sun for the japanese yen 225.22 - 224.77 , I get the answer 1.04 but I want int converting to 104 as an integer value.

The float value comes from a database field.

The round function gives 1

Thanks
Andy
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 11

Expert Comment

by:calinutz
ID: 18041676
You calculate:
225.22 - 224.77
and you get 1.04 ????
I do not follow


The rounding functions can be expanded to different styles of rounding... you just need to show us by example what you need to achieve... (try just with numbers (math))

for example I use this modified function for rounding:

function RoundD(x: Extended; d: Integer): Extended;
// RoundD(123.456, 0) = 123.00
// RoundD(123.456, 2) = 123.46
// RoundD(123456, -3) = 123000
var
  n: Extended;
begin
  n := IntPower(10, d);
  x := x * n;
  Result := (Int(x) + Int(Frac(x) * 2)) / n;
end;


Regards
0
 
LVL 1

Author Comment

by:Asw
ID: 18041895
Sorry should have read
225.22 - 224.18 = 1.04

I need it to read 104
Thanks
Andy
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 18043007
Then multiply the result by 100 and truncate the value....

Russell

0
 
LVL 28

Expert Comment

by:TName
ID: 18043855
As Russel said...

But I'm still somewhat confused. In your first example you said:
>I have some float fields in a database example the yen 127.60  I need to convert this to be left with a round integer value of 128
Which would indeed be rounding, unlike  
SomeInt:=Trunc(AFloat*100);

So do you expect 12760 or 128 in that example?
0
 
LVL 1

Author Comment

by:Asw
ID: 18044188
Hi,

I need to have 127.60 which is a Float field  then convert this to be left with a round integer value of 128.

So do you expect 12760 or 128 in that example?<== 127.60 to 128

Thanks

Andy
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 18045450

Andy, you really need to clarify what your doing. In one comment you indicate that:

>> 1.04 should read 104

and then

>> 127.60 should read 128

The first can only be achieved by * 100 and then Round'ing, the second is achieved through Round'ing only. These are 2 seperate calculations, so what are you looking for?

Russell
0
 
LVL 1

Author Comment

by:Asw
ID: 18045875
Hi Russell,

I need to remove the decimal point then round the number up.

Thanks
Andy
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 18046442
Sorry, I still don't get it; maybe someone else understands what you are trying to do....

Round(127.60) will result in 128
Round(1.04) will result in 1, not 104

Rounding uses the fractional part of the number to either increment (or not) the whole part of the number, the result is an integer value (whole number, no fractions)  that much we can all agree on. The confusion is coming from your statement of "104" from 1.04 . The only way to come up with that is to multiply 1.04 * 100, resulting in 104.00 and then Round that number. If you did the same to 127.60, eg:

127.60 * 100
= 12760.00
Round(12760)
= 12760

The results you are looking for; 104 and 128; cannot be achieved using a single function, as the math you are applying to each is different.

Russell


0
 
LVL 1

Author Comment

by:Asw
ID: 18047474
Hi Russell

Here is what I'm trying to do.

I need to get the number of points betweenthe market open and market close for the currency Euro V USD, the market Low is 1.3121 and the market High is 1.3203.

I need to deduct the high from the low which gives 0.00818169 this is the points from high to low, now I need to round this number to an integer value of 82 with no zeros or decimal point.

Hope this is more clear

Many Thanks for your Help
Andy
0
 
LVL 28

Expert Comment

by:TName
ID: 18048096
Do you always want to shift the decimal point 4 positions to the right?
Is it ok if e.g. 0.000018169 becomes 0?

Do you mean something like this?

function ShiftRound(HighF,LowF:Single):Integer;
begin
   Result:=Round((HighF-LowF)*10000);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
h,l:Single;
begin
   h:=1.3203;
   l:=1.3121;
   Edit1.Text:=IntToStr(ShiftRound(h,l));
end;
0
 
LVL 28

Expert Comment

by:TName
ID: 18048209
Or maybe this, if you always want to subtract the lower  value from the higher value and don't want to sort it out beforehand:

function ShiftRound(f1,f2:Single):Integer;
begin
   if f1>f2 then
     Result:=Round((f1-f2)*10000)
   else if f1<f2 then
       Result:=Round((f2-f1)*10000)
     else
       Result:=0;
end;
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 18048268
Lol, round in one example, * 100 / round in another, and now * 10000 / round.

Andy,

What you are dealing with here is called "scaling". In your first example, no scaling was applied. In the 104 from 1.04 example, a scaling of 2 was applied (10^2), in this last example, the scaling applied was 4 (10^4). The math is easy, but you will need to specify the scaling factor to be applied to the values (each value had a different scale applied).

function ScaleRound(Value: Double; Scale: Integer): Integer;
var  dblScale:      Double;
begin

  // Set scaling factor
  dblScale:=1;

  // Build scale value
  while (Scale > 0) do
  begin
     dblScale:=dblScale * 10;
     Dec(Scale);
  end;

  // Apply scaling and round result
  result:=Round(Value * dblScale);

end;

Example Usage:

  ShowMessage(IntToStr(ScaleRound(127.60, 0)));
  ShowMessage(IntToStr(ScaleRound(1.04, 2)));
  ShowMessage(IntToStr(ScaleRound(0.00818169, 4)));

---

Russell
0
 
LVL 28

Expert Comment

by:TName
ID: 18048512
Thanks, Russel, at last the child has a name ;)

But couldn't the function be simplified to something like:

Result:=Round(Value*Power(10, scale));

Any drawbacks to this?
0
 
LVL 1

Author Comment

by:Asw
ID: 18048636
Hi Guyz,

This is what I'm trying to achive:

Euro V Dollar Days High = 1.3130 Days Low = 1.3030 I need to remove the decimal point and end up with 100 this is the pips spread for the day.
Euro V Sterling Days High = 0.6780 Days Low = 0.6760 I need to remove the decimal point and the zeros and end up with 20 this is the pips spread for the day.
Sterlin V Yen Days High = 227.73 Days Low = 227.48  I need to remove the decimal point and end up with 25 this is the pips spread for the day.

Thanks
Andy
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 18048698
TName,
Nope (on the drawbacks), except I didn't want to drag the Math unit in.

Andy,
Again it comes down to scaling, which we can't determine for you. Your scaling (point spead) is based on the currency being evaluated. The numbers themselves do not determine the scaling factor to apply, its what they represent (currency-wise) that does. YOU need to determine that part.


Russell
0
 
LVL 1

Author Comment

by:Asw
ID: 18048727
Thanks Guyz

Ok I'll go from here and see how I get on, I'll increase the point themn split them.

Thanks for your help.

Andy
0
 
LVL 11

Accepted Solution

by:
calinutz earned 250 total points
ID: 18059273
Why not try to remove the dot from your float value, convert it to integer (to get rid of the starting zeroes) and then insert a dot after two chars of the resulting string, convert it to float and then round it?

Something like:


procedure TForm1.FormCreate(Sender: TObject);
begin
Edit1.Text:=FloatToStr(0.00818169);
end;

Function StripChar(AString : String; AChar : Char) : String;
var
  P : Integer;
  S : String;
begin
  S := AString;
  P := pos(AChar,S);
  While P > 0 do
  begin
    Delete(S,P,1);
    P := pos(AChar,S);
  end;
  Result := s;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  s:string;
begin
s:=FloatToStr(round(StrToInt(StripChar(edit1.Text,'.'))));
If StrLen(PChar(s))>=3 then
s:=s[1]+s[2]+'.'+s[3];
ShowMessage(FloatToStr(Round(StrToFloat(s))));
end;

This seems to be doing the job you described that you needed

Regards
0
 
LVL 1

Author Comment

by:Asw
ID: 18059827
Hi calinutz,

That is exactly what I''m looking to do.

Many Thanks
Andy
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to convert wav to mp3 in delphi 9 197
Delphi XE10 Round Image 2 121
Dynamically Created Query 3 54
How to Get Images From Server using App Tethering 11 20
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now