fromStringOr

Summary

Converts a string to its equivalent signed byte value or the provided default value if the conversion fails.

Usage

public static signed byte fromStringOr(string numberAsStr, signed byte defaultValue)

Returns

The signed byte equivalent of the string value or the provided default value if the string is not valid.

Parameters

numberAsStr

The string value to convert.

defaultValue

The default value to return if the conversion fails.

Description

Converts a string to its equivalent signed byte value or the provided default value if the conversion fails.

import System;

signed byte x = Integer8.fromStringOr("100", 101);
Console.log(x); // 100 // conversion succeeded
signed byte y = Integer8.fromStringOr("abc", 101);
Console.log(y); // 101 // conversion failed so default value returned

If the string being converted is numeric but does not fit into the signed byte data range [ -128, 127 ] (inclusive), the default value will be returned.

import System;

signed byte fits = Integer8.fromStringOr("127", 100);
Console.log(fits); // 127
signed byte exceedByOne = Integer8.fromStringOr("128", 100);
Console.log(exceedByOne); // 100
signed byte exceedByTwo = Integer8.fromStringOr("129", 100);
Console.log(exceedByTwo); // 100
signed byte exceedByThree = Integer8.fromStringOr("130", 100);
Console.log(exceedByThree); // 100

Examples

Basic Usage
1
2
3
4
5
6
import System;
 
signed byte valid = Integer8.fromStringOr("100", 23);
Console.log(valid); // 100
signed byte invalid = Integer8.fromStringOr("abc", 24);
Console.log(invalid); // 24

Share

HTML | BBCode | Direct Link