![]() |
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? |
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; |
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;
|
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? |
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; |
Re: AW: Int32 on input should have specified numbers count on output
This what I have:
Delphi-Quellcode:
Result is always 1 with ADigits 0s?
function NumberDigits(ANumber: Int32; ADigits: Byte = 5): Cardinal;
var A: Cardinal; begin A := Abs(ANumber); Result := Round(A * Power(10, ADigits - Log10(A))); end; Zitat:
// Edited @DeddyH, your function working :D |
AW: Re: AW: Int32 on input should have specified numbers count on output
Zitat:
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; |
Re: Int32 on input should have specified numbers count on output
Maybe Int64 will be also good? and RandgeMin/Max formulas from 1st version?
|
AW: Int32 on input should have specified numbers count on output
Yes, I think so.
|
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 09:37 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz