F# Data Types: An Unsigned Integer



An unsigned integer is a positive natural number from 0 to 4,294,967,295. To declare a variable for such a number, you can use a data type named uint32 and add u to the right side of the value. Here are examples:

let dayOfBirth : uint32 = 8u
let monthOfBirth : uint32 = 11u
let yearOfBirth : uint32 = 1996u

To include an unsigned integer in the call to sprintf, use %u in the placeholder. Here is an example:

open System
open System.Windows.Forms

let exercise = new Form()
exercise.Text <- "Exercise"
exercise.Width <- 140
exercise.Height <- 80
let lblDistance = new Label()

lblDistance.Left <- 28
lblDistance.Top <- 18
lblDistance.Width <- 228

let distance : uint32 = 372u

let strDistance = sprintf "Distance: %u" distance

lblDistance.Text <- strDistance



This would produce:

Unsigned Integers

To convert a number to an unsigned integer, call the uint32() function.

Unsigned Short Integers

An unsigned short integer is a relatively small positive number between 0 and 65535. When declaring a variable for such a value, when initializing it, add a us suffix to its value. Here are examples:

let numberOfTracks = 16us
let musicCategory = 2us

If you want to indicate the data type of the variable, use uint16. Here are examples:

let numberOfTracks:uint16 = 16us;
let musicCategory:uint16 = 2us;

To convert a value to an unsigned short integer, call the uint16() function.

Unsigned Long Integers

An unsigned long integer is a positive number ranging from 0 to very large. To support those types of values, the F# language provides the uint64 data type. When initializing the variable, add UL to its value.


Previous Copyright © 2015 FunctionX Next