SRL Archive - OSR Help

Change last 3 digits of an int to 0?


halgboy_tbc

2013-04-20 22:01:15

How can I change the last 3 digits of an integer to 0.

I am trying to do something such as changing

Num := 750450;

to 750000.


Should be easy to do if it was changed into an array but I do not know how to change an int to an integer array…

Thank you.

wthomas

2013-04-20 22:04:07

Think about simple maths, you just want to round the number to the nearest 1000…
eg.



program new;

var
x: Integer;
begin
x := 123456;
Writeln(x);
x := Round(x / 1000) * 1000;
Writeln(x);
end.

Brandon

2013-04-20 22:06:07

From: halgboy_tbc
How can I change the last 3 digits of an integer to 0.

I am trying to do something such as changing

Num := 750450;

to 750000.


Should be easy to do if it was changed into an array but I do not know how to change an int to an integer array…

Thank you.




Well.. you can use masking.. but just forget I even mentioned it. The simplest way I can think of is either an array or a string which wouldn't involve any math really:


Num := 750450;
SomeString := ToStr(Num);
if (Length(SomeString) > 3) Then
begin
Num := Length(SomeString);
SomeString[Num - 3] := '0';
SomeString[Num - 2] := '0';
SomeString[Num - 1] := '0';
end;

Num := StrToIntDef(SomeString, -1);

OR


var
Num: Integer;
begin
Num := 750450;
Num := Floor(Num / 1000) * 1000;
end.

halgboy_tbc

2013-04-20 22:09:27

From: wthomas
Think about simple maths, you just want to round the number to the nearest 1000…
eg.
you could div by 1000 then cast to string and att the 0's
you could divide the number by 1000 then floor it.


I suspect rounding the quotient and multiplying by 1000 should work, thanks.

From: Brandon
Well.. you can use masking.. but just forget I even mentioned it. The simplest way I can think of is either an array or a string which wouldn't involve any math really:


Num := 750450;
SomeString := ToStr(Num);
if (Length(SomeString) > 3) Then
begin
Num := Length(SomeString);
SomeString[Num - 3] := 0;
SomeString[Num - 2] := 0;
SomeString[Num - 1] := 0;
end;

Num := StrToIntDef(SomeString, -1);

Thanks, good insights there.

wthomas

2013-04-20 22:11:43

I just Edit'd my post with sample code, basically exactly the same edit as Brandon made also :p

If you want 72600 => 73000 use Round(x);

If you want it to be 72000 then use Floor(x) as in Brandon's method