Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Get number digits (https://www.delphipraxis.net/178041-get-number-digits.html)

WojTec 12. Dez 2013 17:00

Delphi-Version: 2010

Get number digits
 
How to get n digit from number, where n is position in numer?

To get all I have:

Delphi-Quellcode:
  while Number > 0 do
  begin
    Digit := Number mod 10;
    Number := Number div 10;
  end;
I need something like this:

Delphi-Quellcode:
function GetDigit(Number: Int64; Index: Byte): Byte

F1

Union 12. Dez 2013 17:22

AW: Get number digits
 
Not performant, but working:
Delphi-Quellcode:
function GetDigit(Number : int64; Index : byte) : byte;
var
   NumStr : string;
begin
   NumStr := IntToStr(Number);
   // Index starts with 0 from the right
   result := StrToInt(copy(NumStr, length(NumStr)-Index, 1));
end;

p80286 12. Dez 2013 17:24

AW: Get number digits
 
Delphi-Quellcode:
function DecDigitFromNumber(number:integer;digpos:byte);
var
  dummy : string;
begin
  dummy:=inttostr(digpos);
  result:=dummy[i];
end;
Just typed in and not tested.

greetings
K-H

DeddyH 12. Dez 2013 17:28

AW: Get number digits
 
What' s your problem exactly? You could simply use a loop and divide by 10 within it. The resulting number mod 10 should give you the value you want. If this does not work, give us a short example of the values and the expected result.

WojTec 12. Dez 2013 17:32

Re: Get number digits
 
Thanks guys, but I forgot about one the most important thing - I don't want string, just math or binary math :wink:

So, for example we have 1234. Now we need get second digit, result should be 2. In my sample I'll get all digits, I need just specified one on specified position in number, not all.

DeddyH 12. Dez 2013 17:55

AW: Get number digits
 
Something like this?
Delphi-Quellcode:
function GetDigit(Number: Int64; Index: Byte): Byte;
var
  temp: int64;
  i, CountDigits: byte;
begin
  temp := Number;
  CountDigits := 0;
  (* Retrieve total count of digits in Number *)
  while temp > 0 do
    begin
      temp := temp div 10;
      inc(CountDigits);
    end;
  (* Divide by 10 until Index is on the right *)
  for i := 1 to CountDigits - Index do
    Number := Number div 10;
  Result := Number mod 10;
end;

WojTec 12. Dez 2013 18:37

Re: Get number digits
 
Yes. But I thought about something more low level, like bits shift, etc. to boost performance. Anyway thanks for this :)

Aphton 12. Dez 2013 18:41

AW: Get number digits
 
Here are the mathematical fucntions you need:

digitCount(x) := ⌈log10(x+1)⌉
extractDigit(x, digitIdx) := ⌊x / 10^(digitCount(x)-digitIdx)⌋ mod 10

(sidenote - "⌈⌉" stands for ceiling, "⌊⌋" for flooring)

Example number = 1234
digitCount(1234) = ⌈log10(1234+1)⌉
= ⌈3.09⌉
= 4

extractDigit(1234, 2) = ⌊1234 / 10^(4 - 2)⌋ mod 10
= ⌊1234 / 10^2⌋ mod 10
= ⌊1234 / 100⌋ mod 10
= ⌊12.34⌋ mod 10
= 12 mod 10
= 2

Edit: This is mathematically as low as it gets.. Cant think of a shorter way right now.
Bitshifting works on base 2. You would have to remap which is an extra overhead that is not needed.
There are assembler instructions (SSE, SSE2, ..) that are faster.
As far as I know the math.pas functions use the optimized assembler operations. Use it!

Der schöne Günther 12. Dez 2013 18:43

AW: Get number digits
 
The numbers you (as a human) are thinking about are decimal numbers. Bitshifting integers is for binary numbers.

WojTec 12. Dez 2013 19:29

Re: Get number digits
 
Wow, math is I looked for, thank you! :-D


Alle Zeitangaben in WEZ +1. Es ist jetzt 00:11 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