cast

Casts a value in a specific type

CAST

NAME

cast - Casts a value in a specific type

SYNOPSIS

(int) (double) (string) (bool)

DESCRIPTION

This function is used to change the type of a variable into another one. The "cast" of an array into a double or an integer returns the number of elements in the array. The "cast" of an array into a string returns the JSON equivalent to this array.

Casting a double to an int might cause some precision issue as seen in this example:

amt = 64.24;

"The amount = "; amt; "\n";

amt *= 100.0;

"multiplied by 100:="; amt;"\n";

amt = (int)amt;

amt;"\n";

Which result in

The amount = 64.24

multiplied by 100:=6424

6423

The correct way to implement this is:

amt = 64.24;

"The amount = "; amt; "\n";

amt *= 100.0;

"multiplied by 100:="; amt;"\n";

amt = round(amt);

amt = (int)amt;

amt;"\n";

EXAMPLES

{{a=undefined; (int) a; }} return 0
{{a=null; (int) a; }} return 0
{{a=[1,2,3]; (int) a; }} return 3
{{a={x:1,y:2}; (int) a; }} return 2
{{a=true; (int) a; }} return 1
{{a=false; (int) a; }} return 0
{{a=7.12345; (int) a; }} return 7
{{a='7.1'; b  = (int) a; b;}} return 7
{{a='7'; b  = (int) a; b;}} return 7
{{a=7; b  = (int) a; b;}} return 7
{{a=7.12345; b  = (int) a; b;}} return 7

{{a=undefined; (float) a; }} return 0
{{a=null; (float) a; }} return 0
{{a=[1,2,3]; (float) a; }} return 3
{{a={x:1,y:2}; (float) a; }} return 2
{{a=true; (float) a; }} return 1
{{a=false; (float) a; }} return 0
{{a=7.12345; (float) a; }} return 7.12345
{{a='7.1'; b  = (float) a; b;}} return 7.1
{{a='7'; b  = (float) a; b;}} return 7
{{a=7; b  = (float) a; b;}} return 7
{{a=7.12345; b  = (float) a; b;}} return 7.12345

{{a=undefined; (bool) a; }} return false
{{a=null; (bool) a; }} return false
{{a=[1,2,3]; (bool) a; }} return true
{{a=[]; (bool) a; }} return false
{{a={x:1,y:2}; (bool) a; }} return true
{{a={}; (bool) a; }} return false
{{a=true; (bool) a; }} return true
{{a=false; (bool) a; }} return false
{{a=7.12345; (bool) a; }} return true
{{a='7.1'; b  = (bool) a; b;}} return true
{{a=''; b  = (bool) a; b;}} return false
{{a='7'; b  = (bool) a; b;}} return true
{{a='0'; b  = (bool) a; b;}} return false
{{a=7; b  = (bool) a; b;}} return true
{{a=7.12345; b  = (bool) a; b;}} return true

{{a=undefined; (string) a; }} return undefined
{{a=null; (string) a; }} return
{{a=[1,2,3]; (string) a; }}",q(\[1,2,3\]),0);
{{a={x:1,y:2}; (string) a; }}",q(.*"x":1.*}),0);
{{a={x:1,y:2}; (string) a; }}",q(.*"y":2.*}),0);
{{a=true; (string) a; }} return true
{{a=false; (string) a; }} return false
{{a=7.12345; (string) a; }} return 7.12345
{{a='7.1'; b  = (string) a; b;}} return 7.1
{{a='7'; b  = (string) a; b;}} return 7
{{a=7; b  = (string) a; b;}} return 7
{{a=7.12345; b  = (string) a; b;}} return 7.12345

SEE ALSO

AUTHOR

Written by Pierre Laplante and Caroline Laplante, <laplante@sednove.com>

MODIFICATIONS

1.0 2014-09-09 21:24:14 laplante@sednove.com

Edit

© 2024 extenso Inc. All rights reserved.