HyperMath

Coercion

Coercion

Previous topic Next topic No expanding text in this topic  

Coercion

Previous topic Next topic JavaScript is required for expanding text JavaScript is required for the print function  

HyperMath automatically converts numbers to strings and vice versa where it is appropriate.  This is called coercion.  The following example illustrates converting numbers to a string:

      SetNumberFormat('long')  

   > print("Pi = " + PI) // Converts the value of pi to string before concatenating

   Pi = 3.1415926535898

   > print("Pi = " + 3.1415927) //Converts 3.1415927 to string

   Pi = 3.1415927

In the example above, during coercion, you do not have full control over the formatting of the conversion.  To format the number as a string, use the StrFormat()function.  For example:

   > print(StrFormat("%.3f", PI) )

   3.142

This is an explicit conversion using a function to convert the number, rather than coercion.