Link to home
Start Free TrialLog in
Avatar of Asw
Asw

asked on

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
Avatar of TName
TName

Hi, why not simply:
Round(SomeFloat);
or
Round(127.60);
Avatar of Asw

ASKER

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
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
Avatar of Asw

ASKER

Sorry should have read
225.22 - 224.18 = 1.04

I need it to read 104
Thanks
Andy
Avatar of Russell Libby
Then multiply the result by 100 and truncate the value....

Russell

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?
Avatar of Asw

ASKER

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

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
Avatar of Asw

ASKER

Hi Russell,

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

Thanks
Andy
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


Avatar of Asw

ASKER

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
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;
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;
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
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?
Avatar of Asw

ASKER

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
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
Avatar of Asw

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of calinutz
calinutz
Flag of Romania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Asw

ASKER

Hi calinutz,

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

Many Thanks
Andy