• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 333
  • Last Modified:

Correctly convert Float values.

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
Asw
Asked:
Asw
  • 8
  • 6
  • 5
  • +1
1 Solution
 
TNameCommented:
Hi, why not simply:
Round(SomeFloat);
or
Round(127.60);
0
 
AswAuthor Commented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
calinutzCommented:
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
 
AswAuthor Commented:
Sorry should have read
225.22 - 224.18 = 1.04

I need it to read 104
Thanks
Andy
0
 
Russell LibbySoftware Engineer, Advisory Commented:
Then multiply the result by 100 and truncate the value....

Russell

0
 
TNameCommented:
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
 
AswAuthor Commented:
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
 
Russell LibbySoftware Engineer, Advisory Commented:

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
 
AswAuthor Commented:
Hi Russell,

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

Thanks
Andy
0
 
Russell LibbySoftware Engineer, Advisory Commented:
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
 
AswAuthor Commented:
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
 
TNameCommented:
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
 
TNameCommented:
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
 
Russell LibbySoftware Engineer, Advisory Commented:
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
 
TNameCommented:
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
 
AswAuthor Commented:
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
 
Russell LibbySoftware Engineer, Advisory Commented:
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
 
AswAuthor Commented:
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
 
calinutzCommented:
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
 
AswAuthor Commented:
Hi calinutz,

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

Many Thanks
Andy
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 8
  • 6
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now