Haxe Code Cookbook
Haxe programming cookbookBeginnerUsing numbers

Using numbers

Reading time: 1.5 minute

Define integers and floats:

var a:Float = 34;  // Float
var b:Int = 34;    // Int
var c = 34.00;     // Float
var d = 34;        // Int

Calculating numbers using arithmetic operators:

var a = 10;
var b = 20;
var c = (a + (2 * b)) / 5;
trace(c); // 10 (Float)

Haxe interprets numeric constants as hexadecimal if they are preceded by 0x:

var value = 0xFF; // 255 (Int)

Extra large or small numbers can be written with scientific exponent notation:

var x = 123e5;    // 12300000
var y = 123e-5;   // 0.00123

Floating point arithmetic is not always 100% accurate

var value = 0.1 + 0.2; // 0.30000000000000004

Creating random numbers:

Std.random(10); // a random Int between 0 (included) and 10 (excluded)
Math.random();  // a random Float between 0.0 (included) and 1.0 (excluded)

Defines infinity. Value equals infinity:

var value = 1 / 0; // infinity

trace((1 / 0) == Math.POSITIVE_INFINITY);  // true
trace((-1 / 0) == Math.NEGATIVE_INFINITY); // true

Defines and check to NaN (Not A Number).

var value = Math.sqrt(-1); // NaN
trace(Math.isNaN(value));  // true

Parsing numbers

Parsing String to Int:

Std.parseInt("3"); // 3
Std.parseInt("3.5"); // 3
Std.parseInt("3 kilo"); // 3
Std.parseInt("kilo: 3.5"); // null

Parsing String to Float:

Std.parseFloat("3"); // 3.0
Std.parseFloat("3.5"); // 3.5
Std.parseFloat("3.5 kilo"); // 3.5
Std.parseFloat("kilo: 3.5"); // Math.NaN

Convert Float to Int:

var value:Float = 3.3;
Std.int(value); // 3
Math.floor(value); // 3
Math.round(value); // 3
Math.ceil(value); // 4

Convert numbers to string:

var myInt = 10;
var myFloat = 10.5;
Std.string(myInt); // "10"
Std.string(myFloat); // "10.5"

Math

Calculating degrees to radians and radians to degrees:

var radians = Math.PI * 2;
var degrees = radians * 180 / Math.PI;
var radians =  degrees * Math.PI / 180;

Using sinus and cosinus to set the position at a distance from the given angle:

var angle = Math.PI;
var distance = 100;
var x = Math.cos(angle) * distance;
var y = Math.sin(angle) * distance;

Calculating the angle of two points:

var point1 = {x: 350, y: 0}
var point2 = {x: 350, y: 150}

var dx = point2.x - point1.x;
var dy = point2.y - point1.y;
var angle = Math.atan2(dy, dx);
trace(angle); // PI/2

API documentation

Manual


Contributors:
Last modified:
Created:
Category:  Beginner
Tags: