Autor Beitrag
sky21
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 141

W7
D2010, XE2
BeitragVerfasst: Do 23.04.09 15:52 
Hi all

Welche eleganten Möglichkeiten gibt es, eine Zahl in einen String mit fixer Länge umzuwandeln?

Also so:

// Länge = 4
123456 -> '1234' (oder alternativ: '3456')
123 -> '0123'

Ich habe mich mit format() rumgeschlagen aber nichts passendes gefunden. Da bleibt mir nur noch die Möglichkeit auf auf die Bereichsprüfung: if (zahl > 9999) then SchneideAb() ...
Regan
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 2157
Erhaltene Danke: 72


Java (Eclipse), Python (Sublimetext 3)
BeitragVerfasst: Do 23.04.09 15:55 
Und wir wäre es mit Copy(IntToStr(DeineZahl),0,4)?
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Do 23.04.09 18:18 
ausblenden Delphi-Quelltext
1:
S := Copy(Format('%.5d', [X]), 1, 4);					

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
sky21 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 141

W7
D2010, XE2
BeitragVerfasst: Fr 24.04.09 12:23 
Super, danke! Für die Fixlänge von 4 müsste der Code allerdings entsprechend '%.4d' sein...