Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Int32 on input should have specified numbers count on output (https://www.delphipraxis.net/155233-int32-input-should-have-specified-numbers-count-output.html)

WojTec 14. Okt 2010 09:48

Delphi-Version: 5

Int32 on input should have specified numbers count on output
 
Hi,

I have another question for you.

On input I have a number <> 0, eg. -542, 4558846, 95423, -55487130 (Int32). On output I want to specified number of input digits whithout converting to string (trim or extend if needed), eg. if digits count = 5 for above numbers output will be:

-55487130 --> 55487 (if < 0 then call Abs())
-542 --> 54200 (if number is < than counter then fill with 0)
4558846 --> 45588
95423 --> 95423

Do you have some idea how can do it?

DeddyH 14. Okt 2010 09:54

AW: Int32 on input should have specified numbers count on output
 
A simple solution:
Delphi-Quellcode:
function Number5Digits(aNumber: integer): Cardinal;
begin
  Result := abs(aNumber);
  while Result > 99999 do
    Result := Result div 10;
  while Result < 10000 do
    Result := Result * 10;
end;

WojTec 14. Okt 2010 12:19

Re: Int32 on input should have specified numbers count on output
 
Thanks :) And what about something like this:

Delphi-Quellcode:
function NumberDigits(ANumber: Int32; ADigits: Byte = 5): Cardinal;
:)

jfheins 14. Okt 2010 12:27

AW: Int32 on input should have specified numbers count on output
 
0. Use Abs()
1. Use log(aNumber) (base 10) to get the number of digits
2. x = ADigits - (result of 1.)
3. Result = ANumber * Math.Power(10, (result of 2.));

Just curious: What ist the reason for this? Output with fixed number of significant figures?

DeddyH 14. Okt 2010 13:06

AW: Int32 on input should have specified numbers count on output
 
Or something like this:
Delphi-Quellcode:
uses math;

function NumberToDigits(aNumber: integer; Digits: Byte = 5): Cardinal;
var RangeMin, RangeMax: Cardinal;
begin
  if Digits < 1 then
    raise Exception.Create('What number do you expect to get having less than 1 digit?');
  Result := abs(aNumber);
  RangeMin := trunc(Power(10,Pred(Digits)));
  RangeMax := Pred(trunc(Power(10,Digits)));
  while Result > RangeMax do
    Result := Result div 10;
  while Result < RangeMin do
    Result := Result * 10;
end;

WojTec 14. Okt 2010 13:15

Re: AW: Int32 on input should have specified numbers count on output
 
This what I have:

Delphi-Quellcode:
function NumberDigits(ANumber: Int32; ADigits: Byte = 5): Cardinal;
var
  A: Cardinal;
begin
  A := Abs(ANumber);
  Result := Round(A * Power(10, ADigits - Log10(A)));
end;
Result is always 1 with ADigits 0s?

Zitat:

Zitat von jfheins (Beitrag 1055748)
with fixed number of significant figures?

Exactly! I'm trying make own serial protection to stop little "crackers" :)

// Edited

@DeddyH, your function working :D

DeddyH 14. Okt 2010 14:39

AW: Re: AW: Int32 on input should have specified numbers count on output
 
Zitat:

Zitat von Woj[I
// Edited[/I]

@DeddyH, your function working :D

Fine, but you should declare RangeMin and RangeMax as Extended to avoid wrong results due to integer overflows (did not think about this before):

Delphi-Quellcode:
function NumberToDigits(aNumber: integer; Digits: Byte = 5): Cardinal;
var RangeMin, RangeMax: Extended;
begin
  if Digits < 1 then
    raise Exception.Create('What number do you expect to get having less than 1 digit?');
  Result := abs(aNumber);
  RangeMin := Power(10,Pred(Digits));
  RangeMax := Power(10,Digits) - 1;
  while Result > RangeMax do
    Result := Result div 10;
  while Result < RangeMin do
    Result := Result * 10;
end;

WojTec 14. Okt 2010 17:29

Re: Int32 on input should have specified numbers count on output
 
Maybe Int64 will be also good? and RandgeMin/Max formulas from 1st version?

DeddyH 14. Okt 2010 17:32

AW: Int32 on input should have specified numbers count on output
 
Yes, I think so.

WojTec 14. Okt 2010 18:29

Re: Int32 on input should have specified numbers count on output
 
I changeg Int32 to Int64 after asked, because I didn't sure range is good or not ;) In my tests all was ok, but I changed type. Thanks for help :)


Alle Zeitangaben in WEZ +1. Es ist jetzt 07:19 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz