# 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
LVL 1
###### Who is Participating?

Commented:
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

Commented:
Hi, why not simply:
Round(SomeFloat);
or
Round(127.60);
0

Author 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

Commented:
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

Author Commented:
225.22 - 224.18 = 1.04

I need it to read 104
Thanks
Andy
0

Then multiply the result by 100 and truncate the value....

Russell

0

Commented:
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

Author 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

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

and then

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

Author Commented:
Hi Russell,

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

Thanks
Andy
0

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

Author 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

Andy
0

Commented:
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

Commented:
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

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

Commented:
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

Author 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

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

Author Commented:
Thanks Guyz

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

Andy
0

Author Commented:
Hi calinutz,

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

Many Thanks
Andy
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.