AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Get number digits

Ein Thema von WojTec · begonnen am 12. Dez 2013 · letzter Beitrag vom 12. Dez 2013
Antwort Antwort
WojTec

Registriert seit: 17. Mai 2007
480 Beiträge
 
Delphi XE6 Professional
 
#1

Get number digits

  Alt 12. Dez 2013, 17:00
Delphi-Version: 2010
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:

function GetDigit(Number: Int64; Index: Byte): Byte
F1
  Mit Zitat antworten Zitat
Benutzerbild von Union
Union

Registriert seit: 18. Mär 2004
Ort: Luxembourg
3.487 Beiträge
 
Delphi 7 Enterprise
 
#2

AW: Get number digits

  Alt 12. Dez 2013, 17:22
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;
Ibi fas ubi proxima merces
sudo /Developer/Library/uninstall-devtools --mode=all
  Mit Zitat antworten Zitat
Benutzerbild von p80286
p80286

Registriert seit: 28. Apr 2008
Ort: Stolberg (Rhl)
6.659 Beiträge
 
FreePascal / Lazarus
 
#3

AW: Get number digits

  Alt 12. Dez 2013, 17:24
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
Programme gehorchen nicht Deinen Absichten sondern Deinen Anweisungen
R.E.D retired error detector
  Mit Zitat antworten Zitat
Benutzerbild von DeddyH
DeddyH

Registriert seit: 17. Sep 2006
Ort: Barchfeld
27.542 Beiträge
 
Delphi 11 Alexandria
 
#4

AW: Get number digits

  Alt 12. Dez 2013, 17:28
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.
Detlef
"Ich habe Angst vor dem Tag, an dem die Technologie unsere menschlichen Interaktionen übertrumpft. Die Welt wird eine Generation von Idioten bekommen." (Albert Einstein)
Dieser Tag ist längst gekommen
  Mit Zitat antworten Zitat
WojTec

Registriert seit: 17. Mai 2007
480 Beiträge
 
Delphi XE6 Professional
 
#5

Re: Get number digits

  Alt 12. Dez 2013, 17:32
Thanks guys, but I forgot about one the most important thing - I don't want string, just math or binary math

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.
  Mit Zitat antworten Zitat
Benutzerbild von DeddyH
DeddyH

Registriert seit: 17. Sep 2006
Ort: Barchfeld
27.542 Beiträge
 
Delphi 11 Alexandria
 
#6

AW: Get number digits

  Alt 12. Dez 2013, 17:55
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;
Detlef
"Ich habe Angst vor dem Tag, an dem die Technologie unsere menschlichen Interaktionen übertrumpft. Die Welt wird eine Generation von Idioten bekommen." (Albert Einstein)
Dieser Tag ist längst gekommen
  Mit Zitat antworten Zitat
WojTec

Registriert seit: 17. Mai 2007
480 Beiträge
 
Delphi XE6 Professional
 
#7

Re: Get number digits

  Alt 12. Dez 2013, 18:37
Yes. But I thought about something more low level, like bits shift, etc. to boost performance. Anyway thanks for this
  Mit Zitat antworten Zitat
Benutzerbild von Aphton
Aphton

Registriert seit: 31. Mai 2009
1.198 Beiträge
 
Turbo Delphi für Win32
 
#8

AW: Get number digits

  Alt 12. Dez 2013, 18:41
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!
das Erkennen beginnt, wenn der Erkennende vom zu Erkennenden Abstand nimmt
MfG

Geändert von Aphton (12. Dez 2013 um 19:09 Uhr)
  Mit Zitat antworten Zitat
Der schöne Günther

Registriert seit: 6. Mär 2013
6.110 Beiträge
 
Delphi 10 Seattle Enterprise
 
#9

AW: Get number digits

  Alt 12. Dez 2013, 18:43
The numbers you (as a human) are thinking about are decimal numbers. Bitshifting integers is for binary numbers.
  Mit Zitat antworten Zitat
WojTec

Registriert seit: 17. Mai 2007
480 Beiträge
 
Delphi XE6 Professional
 
#10

Re: Get number digits

  Alt 12. Dez 2013, 19:29
Wow, math is I looked for, thank you!
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 08:57 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