View Category

Find the distance between two points

fantom
px1 := 34.0f; py1 := 78.0f; px2 := 67.0f; py2 := -45.0f
distance := |Float x1, Float y1, Float x2, Float y2 -> Float|
{ ((x2-x1).pow(2.0f) + (y2-y1).pow(2.0f)).sqrt }

distance(px1, py1, px2, py2)

Zero pad a number

Given the number 42, pad it to 8 characters like 00000042
fantom
formatted := 42.toStr.padl(8, '0')
formatted := 42.toLocale("00000000")

Right Space pad a number

Given the number 1024 right pad it to 6 characters "1024  "
fantom
formatted := 1024.toStr.padr(6)

Format a decimal number

Format the number 7/8 as a decimal with 2 places: 0.88
fantom
formatted := (7.0/8.0).toLocale("0.00")

Left Space pad a number

Given the number 73 left pad it to 10 characters "        73"
fantom
formatted := 73.toStr.padl(10)

Generate a random integer in a given range

Produce a random integer between 100 and 200 inclusive
fantom
r := Int.random(100..200)

Generate a repeatable random number sequence

Initialise a random number generator with a seed and generate five decimal values. Reset the seed and produce the same values.
fantom
rand := Random.makeSeeded(12345)
first := Int[,].fill(0,5).map { rand.next(100..200) }

rand2 := Random.makeSeeded(12345)
second := Int[,].fill(0,5).map { rand2.next(100..200) }