Skip to content

SpecificationXII · Library

12.3

Conversions

Conversions change a value's type explicitly: from number to text, from text to number, or from real to text with a fixed number of decimals.

Markdown version

12.3.1 How it works

PseudoLearn never converts on its own. When needed, you ask:

FunctionConverts
toText(v)any primitive to String, with the same format as Write
toInteger(t)a decimal string to Integer
toReal(t)a decimal string to Real
decimals(x, n)a number to text with exactly n decimals
Figure 12.3-1.Conversions go back and forth between text and numbers. If the text is not a number, the conversion fails instead of making up a zero.

At the top, the text "42" becomes a number with toInteger and comes back with toText. Below, the text "doce" cannot be converted and toInteger fails.

ProgramConverting both ways

Algorithm Conversiones
  Write toText(12) + "!", " ", toInteger("42") + 1, " ", toReal("2.5") * 2;
  Write decimals(3.14159, 2), " ", decimals(2.5, 0), " ", decimals(-0.001, 1);
EndAlgorithm

Output

12! 43 5.0
3.14 3 -0.0

The program finishes normally.

decimals rounds ties away from zero, keeps the sign —that is why -0.001 with one decimal is -0.0— and uses no thousands separators.

In short

  • Every conversion is explicit.
  • toText, toInteger and toReal go between text and numbers.
  • A text that is not a number fails; it does not become zero.
  • decimals fixes a number's decimals.