AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Sprachen und Entwicklungsumgebungen Object-Pascal / Delphi-Language Delphi Int32 on input should have specified numbers count on output
Thema durchsuchen
Ansicht
Themen-Optionen

Int32 on input should have specified numbers count on output

Ein Thema von WojTec · begonnen am 14. Okt 2010 · letzter Beitrag vom 14. Okt 2010
Antwort Antwort
WojTec

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

Int32 on input should have specified numbers count on output

  Alt 14. Okt 2010, 09:48
Delphi-Version: 5
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?
  Mit Zitat antworten Zitat
Benutzerbild von DeddyH
DeddyH

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

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

  Alt 14. Okt 2010, 09:54
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;
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
 
#3

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

  Alt 14. Okt 2010, 12:19
Thanks And what about something like this:

function NumberDigits(ANumber: Int32; ADigits: Byte = 5): Cardinal;
  Mit Zitat antworten Zitat
Benutzerbild von jfheins
jfheins

Registriert seit: 10. Jun 2004
Ort: Garching (TUM)
4.579 Beiträge
 
#4

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

  Alt 14. Okt 2010, 12:27
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?
  Mit Zitat antworten Zitat
Benutzerbild von DeddyH
DeddyH

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

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

  Alt 14. Okt 2010, 13:06
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;
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
 
#6

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

  Alt 14. Okt 2010, 13:15
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?

with fixed number of significant figures?
Exactly! I'm trying make own serial protection to stop little "crackers"

// Edited

@DeddyH, your function working

Geändert von WojTec (14. Okt 2010 um 13:36 Uhr)
  Mit Zitat antworten Zitat
Benutzerbild von DeddyH
DeddyH

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

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

  Alt 14. Okt 2010, 14:39
Zitat von Woj[I:
// Edited[/I]

@DeddyH, your function working
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;
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
 
#8

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

  Alt 14. Okt 2010, 17:29
Maybe Int64 will be also good? and RandgeMin/Max formulas from 1st version?
  Mit Zitat antworten Zitat
Benutzerbild von DeddyH
DeddyH

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

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

  Alt 14. Okt 2010, 17:32
Yes, I think so.
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
 
#10

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

  Alt 14. Okt 2010, 18:29
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
  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 15:02 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