Manage Learn to apply best practices and optimize your operations.

Convert number value to text string (R5 and earlier)

This code will convert a number value to a text string.

This code will convert a number value to a text string.

Simply paste this code in a "Computed" field and replace "number" in the setup area to the field you want to convert. This is an international version that is compatible with any language using the "hundreds tens ones" ("nine hundred ninety nine")format for specifying numbers. The code is REMed for locations to get the wording correct, including international separators for the divisions and decimal and whether the number should be treated as currency or the decimals should be text ("ninety nine cents") or a fraction ("99/100 dollars").

It's current limitation is 999,999,999, but that can be extended to include larger numbers by copying a division, adding it to the end of the code and then adding the new division's text value to the "divs" variable.

A huge thanks goes to Jason Horton for testing and suggesting improvements.

  
  Code: REM "***** Setup *****";
numfield := @Text(number);
intlcurrency := " dollars"; REM "the currency to use";
intlformat := " ";     REM "the text separater 
between hundreds and tens";
intlnumsep := "and ";  
REM "the text separater between whole and 
decimal numbers";
intlnumCtxt := " hundred "; 
REM "the text value of hundred for each 
division";
intldivsep := ",";    REM "the numeric separator 
between divisions of numbers";
intldecsep := ".";    REM "the numeric separator 
between whole and decimal numbers";
intldectxt := " cents";   
REM "the text for decimal currency (" 
pence"; "/100"; ...)";
iscurrency := @True;  
REM "whether the number should be currency 
(@True; @False)";
isdectxt := @True;   
REM "whether the decimals should be 
text or numeric (@True; @False)";
divs := ", thousand, million, billion";
single := ",one,two,three,four,five,six,seven,eight,nine";
double := 
"eleven,twelve,thirteen,fourteen,
fifteen,sixteen,seventeen,eighteen,ni
neteen";
tens := "ten,twenty,thirty,forty,
fifty,sixty,seventy,eighty,ninety";

REM "***** Code Preparation *****";
@If(numfield = ""; @Return(""); "");
fullnum := @Explode(numfield; 
intldecsep); thisnum := @Subset(fullnum; 1); 
thisdec := @If(@Elements
(fullnum) > 1; @Subset(fullnum; -1); ""); @If
(@IsNumber(thisnum); ""; 
@TextToNumber(number)); a := 
@Implode(@Explode(@Text
(thisnum; "F0"); intldivsep); ""); b := 
@Length(a); divcount := 1; ishundred := 
b > 2; isthousand := b > 3; 
ishthousand := b > 5; ismillion := b > 
6; ishmillion := b > 8; isbillion := b > 9;

REM "***** Start of Decimals *****";
hasdecimal := !(thisdec = "");
txtval := @If(hasdecimal; 
@Left(thisdec + "0"; 2); ""); numval := @If
(hasdecimal; @TextToNumber(txtval); ""); 
oneval := @If(hasdecimal; @TextToNumber
(@Right(txtval; 1)); ""); tenval := 
@If(hasdecimal; @TextToNumber(@Left(txtval; 
1)); ""); newstring := @If(hasdecimal;
 @If(isdectxt;
  @If(numval > 19; 
@Word(tens; ","; tenval) + " " + @Word
(single; ","; oneval + 1);
   numval > 10; @Word(double; ","; oneval);
   numval = 10; @Word(tens; ","; 1);
   @Word(single; ","; oneval + 1)
   );
  txtval)
   + @If(numval = 0; ""; intldectxt);
 "");
numstring := @If(hasdecimal 
& numval > 0; intlnumsep; "") + newstring + @If
(isdectxt; ""; intlcurrency);

REM "***** Start of Hundreds *****";
@Set("txtval"; @Right("0" + a; 2));
@Set("numval"; @TextToNumber(txtval));
@Set("oneval"; @TextToNumber
(@Right(txtval; 1))); @Set("tenval"; 
@TextToNumber
(@Left(txtval; 1)));

@Set("newstring"; @If(numval > 19; 
@Word(tens; ","; tenval) + " " + @Word
(single; ","; oneval + 1);
 numval > 10; @Word(double; ","; oneval);
 numval = 10; @Word(tens; ","; 1);
 @Word(single; ","; oneval + 1)
 )
 + @Word(divs; ","; divcount));
@Set("numstring"; newstring + 
@If(iscurrency & isdectxt; intlcurrency; "") 
+ " " + numstring); 
@If(b > ((divcount *3) - 1); 
@Set("numstring"; 
intlformat + numstring); 
@Return(@Trim(numstring)));

@Set("a"; @Left(a; @Length(a) - 2));
@Set("txtval"; @Right(a; 1));
@Set("oneval"; @TextToNumber(txtval));
@Set("newstring"; @If(oneval = 0; ""; 
@Word(single; ","; oneval + 1) + 
intlnumCtxt)); @Set("numstring"; 
newstring + " " + numstring); @If(b > 
(divcount *3); ""; @Return(@Trim(numstring)));

REM "***** Start of Thousands *****";
@Set("divcount"; divcount + 1);
@Set("a"; @Left(a; @Length(a) -1));
@Set("txtval"; @Right("0" + a; 2));
@Set("numval"; @TextToNumber(txtval));
@Set("oneval"; @TextToNumber
(@Right(txtval; 1))); @Set("tenval"; @TextToNumber
(@Left(txtval; 1)));

@Set("newstring";
 @If(numval > 19; 
@Word(tens; ","; tenval) + " " + @Word(single; ","; 
oneval + 1);
  numval > 10; @Word(double; ","; oneval);
  numval = 10; @Word(tens; ","; 1);
  @Word(single; ","; oneval + 1)
  )
  + @Word(divs; ","; divcount)
 );
@Set("numstring"; 
newstring + " " + numstring); 
@If(b > ((divcount *3) - 1); 
@Set("numstring"; intlformat + 
numstring); @Return(@Trim(numstring)));

@Set("a"; @Left(a; @Length(a) - 2));
@Set("txtval"; @Right(a; 1));
@Set("oneval"; @TextToNumber(txtval));
@Set("newstring"; @If(oneval = 0; ""; 
@Word(single; ","; oneval + 1) + 
intlnumCtxt)); @Set("numstring"; 
newstring + " " + numstring); @If(b > 
(divcount *3); ""; @Return(@Trim(numstring)));

REM "***** Start of Millions *****";
@Set("divcount"; divcount + 1);
@Set("a"; @Left(a; @Length(a) -1));
@Set("txtval"; @Right("0" + a; 2));
@Set("numval"; @TextToNumber(txtval));
@Set("oneval"; @TextToNumber
(@Right(txtval; 1))); @Set("tenval"; @TextToNumber
(@Left(txtval; 1)));

@Set("newstring";
 @If(numval > 19; 
@Word(tens; ","; tenval) + " " + @Word(single; ","; 
oneval + 1);
  numval > 10; @Word(double; ","; oneval);
  numval = 10; @Word(tens; ","; 1);
  @Word(single; ","; oneval + 1))
  + @Word(divs; ","; divcount)
 );
@Set("numstring"; newstring + " " + 
numstring); @If(b > ((divcount *3) - 1); 
@Set("numstring"; intlformat + numstring); 
@Return(@Trim(numstring)));

@Set("a"; @Left(a; @Length(a) - 2));
@Set("txtval"; @Right(a; 1));
@Set("oneval"; @TextToNumber(txtval));
@Set("newstring"; @If(oneval = 0; ""; 
@Word(single; ","; oneval + 1) + 
intlnumCtxt)); @Set("numstring"; newstring
 + " " + numstring); @If(b > 
(divcount *3); ""; @Return
(@Trim(numstring)));

REM "***** Start of Billions *****";
@Set("divcount"; divcount + 1);
"Over 999,999,999"

Do you have comments on this tip? Let us know.

This tip was submitted to the SearchDomino.com tip exchange by member Robert Albritton. Please let others know how useful it is via the rating scale below. Do you have a useful Notes/Domino tip or code to share? Submit it to our monthly tip contest and you could win a prize and a spot in our Hall of Fame.

Dig Deeper on Lotus Domino Server 5

SearchWindowsServer

Search400

  • Favorite iSeries cheat sheets

    Here you'll find a collection of valuable cheat sheets gathered from across the iSeries/Search400.com community. These cheat ...

  • HTML cheat sheet

    This is a really cool cheat sheet if you're looking to learn more about HTML. You'll find just about everything you every wanted ...

  • Carol Woodbury: Security

    Carol Woodbury

SearchDataCenter

SearchContentManagement

Close