LessonsAdvanced: Bigger programsLesson 39

Formatting text and numbers

Show numbers with the decimals you want, pad and line up columns with format, round correctly, and fill templates with merge.

  • Advanced
  • Lesson 39 of 83
  • About 9 minutes
  • 8 examples

How many decimals?

When xTalk turns a number into text, to show it or put it in a field, it uses the numberFormat: up to six decimals unless you say otherwise. Set it to a pattern of 0 (always show a digit) and # (show a digit only if needed):

ExampleRun it in the playground
put 22 / 7
set the numberFormat to "0.00"
put 22 / 7
put 2 + 0
set the numberFormat to "0.###"
put 22 / 7
put 2.5 + 0
Output
3.142857
3.14
2.00
3.143
2.5

The numberFormat changes only how a number becomes text. The number itself keeps all its precision:

ExampleRun it in the playground
set the numberFormat to "0.0"
put 1 / 3 into tThird
put tThird
set the numberFormat to "0.######"
put tThird * 3
Output
0.3
1

Rounding

round rounds to the nearest whole number, or to as many decimals as you ask. Halves go away from zero. A negative number of places rounds to tens, hundreds and so on. trunc just cuts the decimals off.

ExampleRun it in the playground
put round(2.5) && round(3.5) && round(-2.5)
put round(3.14159, 2)
put round(1234.5, -2)
put trunc(-3.7) && trunc(3.7)
Output
3 4 -3
3.14
1200
-3 3

Fill in the blanks: format

format builds text from a template, like printf in C. Each % code in the template is replaced by the next value: %d a whole number, %f a number with decimals, %s text, %x a number in hexadecimal.

ExampleRun it in the playground
put format("%d items at %.2f each", 3, 4.5)
put format("%s is %d years old", "Ada", 36)
put format("%05d", 42)
put format("%x in hex is %X", 255, 255)
put format("100%%")
Output
3 items at 4.50 each
Ada is 36 years old
00042
ff in hex is FF
100%

A number between % and the letter is a width, which pads the value with spaces; a minus sign pads on the right instead of the left. That is how to line up columns in plain text:

ExampleRun it in the playground
put "Ada,36.5" & return & "Grace,112" & return & "Linus,7.25" into tRows
repeat for each line tRow in tRows
   put format("%-8s|%8.2f", item 1 of tRow, item 2 of tRow)
end repeat
Output
Ada     |   36.50
Grace   |  112.00
Linus   |    7.25

format also turns \n into a new line and \t into a tab, so it is a compact way to write those inside a string.

Thousands separators

There is no built-in pattern for 1,234,567, but a short function does it, and shows chunk expressions at work:

ExampleRun it in the playground
function withCommas pNumber
   put trunc(pNumber) into tWhole
   put empty into tOut
   repeat while the length of tWhole > 3
      put comma & char -3 to -1 of tWhole before tOut
      delete char -3 to -1 of tWhole
   end repeat
   return tWhole & tOut
end withCommas

put withCommas(1234567)
put withCommas(999)
put withCommas(1000)
Output
1,234,567
999
1,000

Templates: merge

merge fills a template with the values of expressions written between double square brackets. It is the easiest way to build a long piece of text, an email or a web page, with values in it:

ExampleRun it in the playground
put "Ada" into tName
put 36 into tAge
put merge("Hello [[tName]], next year you will be [[tAge + 1]].")
put merge("[[toUpper(tName)]] has [[the length of tName]] letters.")
Output
Hello Ada, next year you will be 37.
ADA has 3 letters.

Try it yourself

Print a receipt: for each line of Tea,2,1.8, Cake,1,3.25 and Juice,3,2 (name, quantity, price), put the name padded to 8 characters, the quantity, and the line's total with two decimals, then a total line.

Show a solution
ExampleRun it in the playground
put "Tea,2,1.8" & return & "Cake,1,3.25" & return & "Juice,3,2" into tOrder
put 0 into tTotal
repeat for each line tLine in tOrder
   put item 2 of tLine * item 3 of tLine into tCost
   add tCost to tTotal
   put format("%-8s x%d %7.2f", item 1 of tLine, item 2 of tLine, tCost)
end repeat
put format("%-11s %7.2f", "Total", tTotal)
Output
Tea      x2    3.60
Cake     x1    3.25
Juice    x3    6.00
Total         12.85

Words in this lesson

Each one finds every lesson that uses it. The Dictionary has the whole story of each, with its syntax and more examples.