Type conversion: Number to string

The programming language Chora supports different data types to represent numerical operands. We distinguish between signed and unsigned integer data types (each with 8-, 16-, 32- or 64-bit precision) and the floating-point data type.

On the other hand, Chora provides the data type string intended to represent text. In order to convert a numerical operand in a string a set of adequate conversion operators is available. The conversion operators are implemented as instant constructors of the data type string.

Explicit conversion

With the explicit conversion you specify explicitly how the numerical operand should be converted in a string. The for this purpose used instant constructors permit you to specify in optional parameters how the number should be converted. For example, you can determine the desired precision (decimal signs) in the resulting string:

var float  a  = 13.69;
var string b1 = string( a, 0, 2 ); // b1 = "13.69"
var string b2 = string( a, 0, 1 ); // b2 = "13.7"

Implicit conversion

Wherever it is not ambiguous, the Chora compiler applies the necessary type conversion implicitly. This is usually the case when you assign the result of an expression to e.g. a variable or you pass it as parameter in a method invocation. In such cases the Chora compiler can derive from the destination operand the corresponding data type and decide whether or not to convert the expression.

For example, the following assignment implies the conversion from float to string data type since the destination operand str is a local variable declared with string as data type. Please note, the implicit conversion results in a shortest possible string representation of the original number by maintaining the maximum precision of the floating-point data type:

var float  a = 13.69;
var string str;

// Apply an implicit conversion from 'float' to 'string'
str = a;  // str = "13.68999958"