Mega Code Archive

 
Categories / Delphi / Strings
 

Converting a double into a string representation

Title: Converting a double into a string representation Question: How do I take a double-precision number and return it in textual form? For example, 101.50 becomes: ONE HUNDRED ONE AND 50/100 Answer: I wrote this code a couple of years ago for a Check Printing routine. I've learned a lot since then, and I'm certain it could be improved upon. However, it works right up to one trillion - 1 function TCheckPrinter.DescAmount(amount: Double): String; var sAmount : string; // String value of the amount. sResult : string; // Working string to return. iLenAmt : integer; // Length of the amount string. iExponent : integer; // Order of magnitude of the amount // (hundred, thousand, etc.) iCounter : integer; // Generic Counter. pStringStart: pchar; // Pointer to read sAmount from left to // right. pStringEnd : pchar; // Pointer to read sAmount from right // to left. procedure Swap( var C1: char; var C2: char ); var cTemp : char; begin cTemp := C1; C1 := C2; C2 := cTemp; end; function ExpText( sValue: string ): string; var sResult : string; iLenVal : integer; iChrVal : integer; begin iLenVal := Length( sValue ); sResult := ''; // Calculate hundreds amount if ( iLenVal 2 ) then begin iChrVal := StrToInt( sValue[ 3 ] ); if ( iChrVal 0 ) then sResult := sResult + sOnes[ iChrVal ] + ' HUNDRED '; end; // Calculate tens and ones. if ( iLenVal 1 ) then begin iChrVal := StrToInt( sValue[ 2 ] ); if ( iChrVal 1 ) then begin sResult := sResult + sTens[ iChrVal ]; iChrVal := StrToInt( sValue[ 1 ] ); if ( iChrVal 0 ) then sResult := sResult + '-' + sOnes[ iChrVal ] + ' ' else sResult := sResult + ' '; end else if ( iChrVal = 1 ) then begin iChrVal := StrToInt( sValue[ 1 ] ); if ( iChrVal 0 ) then sResult := sResult + sTeen[ iChrVal ] + ' ' else sResult := sResult + sTens[ 1 ] + ' '; end else begin iChrVal := StrToInt( sValue[ 1 ] ); if ( iChrVal 0 ) then sResult := sResult + sOnes[ iChrVal ] + ' '; end; end; // Calculate ones. if ( iLenVal = 1 ) then begin iChrVal := StrToInt( sValue[ 1 ] ); if ( iChrVal 0 ) then sResult := sResult + sOnes[ iChrVal ] + ' '; end; Result := sResult; end; begin sAmount := Format( '%-1.2f', [ Amount ] ); iLenAmt := Length( sAmount ); iExponent := 0; // Determine the Magnitude (power of 100) case ( iLenAmt ) of 4..6 : iExponent := 1; 7..9 : iExponent := 2; 10..12: iExponent := 3; 13..15: iExponent := 4; end; // Reverse the order of sAmount. pStringStart := pchar( sAmount ); pStringEnd := pchar( pStringStart + iLenAmt - 1 ); iCounter := 1; while not( iCounter ( iLenAmt div 2 ) ) do begin Swap( pStringStart^, pStringEnd^ ); inc( pStringStart ); dec( pStringEnd ); inc( iCounter ); end; // Step through the groups of three integer values ( 100's, // 1000's, etc. ) iCounter := iExponent; while not( iCounter = 0 ) do begin case ( iCounter ) of 4: sResult := sResult + ExpText( Copy( sAmount, 13, 3 ) ) + 'BILLION '; 3: sResult := sResult + ExpText( Copy( sAmount, 10, 3 ) ) + 'MILLION '; 2: sResult := sResult + ExpText( Copy( sAmount, 7, 3 ) ) + 'THOUSAND '; 1: sResult := sResult + ExpText( Copy( sAmount, 4, 3 ) ); end; dec( iCounter ); end; Result := sResult + 'AND ' + sAmount[ 2 ] + sAmount[ 1 ] + '/100'; end; Ghannodahn Kirrel http://www.icitadel.com