ZetCode

F# data types

last modified December 2, 2026

In this article we cover the basic data types of F#: integers, floating-point numbers, decimals, Booleans, characters and strings.

Every value in F# has a type, and the type is fixed when the program is compiled. The compiler usually works out the type from the value itself, so we rarely have to write it down. The F# types are .NET types: int is another name for System.Int32, float for System.Double, and string for System.String. Values of these types can therefore be passed to any .NET library.

Integer types

F# has signed and unsigned integers of 8, 16, 32 and 64 bits. A plain integer literal is an int; the other types are chosen with a suffix after the number.

integers.fsx
let small = 200uy
let short = -1200s
let regular = 42
let big = 9_000_000_000L
let unsigned = 4_000_000_000u

printfn "byte: %d" small
printfn "int16: %d" short
printfn "int: %d" regular
printfn "int64: %d" big
printfn "uint32: %d" unsigned

We bind integers of five different types.

let small = 200uy
let short = -1200s

The uy suffix makes a byte, an unsigned 8-bit integer, and s makes an int16. The other suffixes are y for sbyte, us for uint16, u for uint32, L for int64 and UL for uint64.

let big = 9_000_000_000L

Underscores make long numbers easier to read; the compiler ignores them. The value does not fit in an int, so it needs the L suffix; without it the program does not compile. The %d placeholder prints integers of any type.

λ dotnet fsi integers.fsx
byte: 200
int16: -1200
int: 42
int64: 9000000000
uint32: 4000000000

Ranges and sizes

Each integer type has a fixed size and a range of values. The ranges are available as the MinValue and MaxValue fields of the .NET types, and the sizeof function returns the size of a type in bytes.

ranges.fsx
printfn "byte:  %d to %d" System.Byte.MinValue System.Byte.MaxValue
printfn "sbyte: %d to %d" System.SByte.MinValue System.SByte.MaxValue
printfn "int16: %d to %d" System.Int16.MinValue System.Int16.MaxValue
printfn "int:   %d to %d" System.Int32.MinValue System.Int32.MaxValue
printfn "int64: %d to %d" System.Int64.MinValue System.Int64.MaxValue

printfn "sizeof<byte>  = %d" sizeof<byte>
printfn "sizeof<int16> = %d" sizeof<int16>
printfn "sizeof<int>   = %d" sizeof<int>
printfn "sizeof<int64> = %d" sizeof<int64>

The program prints the range and the size of four signed integer types and of byte.

printfn "int:   %d to %d" System.Int32.MinValue System.Int32.MaxValue

System.Int32 is the .NET name of the int type. The signed types use one bit for the sign, so their range is split between negative and positive numbers.

printfn "sizeof<int>   = %d" sizeof<int>

The type is passed to sizeof in angle brackets. An int takes 4 bytes and an int64 8 bytes.

λ dotnet fsi ranges.fsx
byte:  0 to 255
sbyte: -128 to 127
int16: -32768 to 32767
int:   -2147483648 to 2147483647
int64: -9223372036854775808 to 9223372036854775807
sizeof<byte>  = 1
sizeof<int16> = 2
sizeof<int>   = 4
sizeof<int64> = 8

Overflow and big integers

Integer arithmetic is not checked for overflow by default. When a result does not fit into its type, it wraps around. For numbers of any size, F# has the bigint type.

overflow.fsx
let maxInt = System.Int32.MaxValue
let next = maxInt + 1

printfn "max int: %d" maxInt
printfn "max int + 1: %d" next

let maxByte = 255uy
let wrapped = maxByte + 1uy

printfn "max byte: %d" maxByte
printfn "max byte + 1: %d" wrapped

let large = 9_223_372_036_854_775_807I
let larger = large * large

printfn "bigint: %A" larger

We add one to the largest int and the largest byte, and multiply two large bigint values.

let maxInt = System.Int32.MaxValue
let next = maxInt + 1

The sum wraps around to the smallest int. The same happens with a byte: 255 plus 1 gives 0.

let large = 9_223_372_036_854_775_807I
let larger = large * large

The I suffix creates a bigint. Its size grows as needed, so the product cannot overflow. The %A placeholder prints a value of any type in F# notation.

λ dotnet fsi overflow.fsx
max int: 2147483647
max int + 1: -2147483648
max byte: 255
max byte + 1: 0
bigint: 85070591730234615847396907784232501249

Floating-point and decimal types

The float type is a 64-bit binary floating-point number and float32 a 32-bit one. The decimal type stores 28 to 29 significant digits in base ten, which makes it the right choice for money.

floats.fsx
let distance = 384_400.0
let ratio = 0.75f
let price = 19.99m

printfn "float: %f" distance
printfn "float32: %f" ratio
printfn "decimal: %M" price

let a = 0.1 + 0.2
let b = 0.1m + 0.2m

printfn "0.1 + 0.2 as float: %.17f" a
printfn "0.1 + 0.2 as decimal: %M" b
printfn "equal to 0.3: %b %b" (a = 0.3) (b = 0.3m)

printfn "1.0 / 0.0 = %f" (1.0 / 0.0)
printfn "sizeof<float> = %d, sizeof<decimal> = %d" sizeof<float> sizeof<decimal>

We create values of the three types and compare how they add 0.1 and 0.2.

let distance = 384_400.0
let ratio = 0.75f
let price = 19.99m

A literal with a decimal point or an exponent is a float. The f suffix makes a float32 and m a decimal. The %f placeholder prints floats with six decimal places; %M prints a decimal.

let a = 0.1 + 0.2
let b = 0.1m + 0.2m

The binary type cannot represent 0.1 exactly, so the float sum is slightly off and is not equal to 0.3. The decimal sum is exact. The %.17f placeholder prints 17 decimal places.

printfn "1.0 / 0.0 = %f" (1.0 / 0.0)

Floating-point division by zero does not fail; it gives infinity. Integer division by zero, on the other hand, stops the program with an error.

λ dotnet fsi floats.fsx
float: 384400.000000
float32: 0.750000
decimal: 19.99
0.1 + 0.2 as float: 0.30000000000000004
0.1 + 0.2 as decimal: 0.3
equal to 0.3: false true
1.0 / 0.0 = Infinity
sizeof<float> = 8, sizeof<decimal> = 16

Booleans and characters

The bool type has the values true and false. A char is a single UTF-16 code unit, written in single quotes.

bools_chars.fsx
let sunny = true
let raining = false

printfn "sunny: %b, raining: %b" sunny raining

let letter = 'F'
let digit = '7'
let quote = '\''

printfn "letter: %c" letter
printfn "digit: %c" digit
printfn "quote: %c" quote

printfn "code of %c: %d" letter (int letter)
printfn "char with code 97: %c" (char 97)
printfn "sizeof<char> = %d" sizeof<char>

We print two Booleans and several characters.

let quote = '\''

A backslash starts an escape sequence. \' is the single quote; others are \n for a new line, \t for a tab, and \\ for the backslash itself.

printfn "code of %c: %d" letter (int letter)
printfn "char with code 97: %c" (char 97)

The int function returns the numeric code of a character, and the char function turns a code into a character. A character takes two bytes.

λ dotnet fsi bools_chars.fsx
sunny: true, raining: false
letter: F
digit: 7
quote: '
code of F: 70
char with code 97: a
sizeof<char> = 2

Strings

A string is an immutable sequence of characters. F# has normal, verbatim, and triple-quoted string literals.

strings.fsx
let word = "falcon"
let path = @"C:\Users\lucia\docs"
let poem = """She said "hi"
and left."""

printfn "%s has %d letters" word word.Length
printfn "first letter: %c" word[0]
printfn "%s" (word + " and hawk")
printfn "%s" path
printfn "%s" poem

We create three strings and use a few of their basic operations.

let path = @"C:\Users\lucia\docs"

In a verbatim string, which starts with @, a backslash is an ordinary character. This is handy for Windows paths and regular expressions.

let poem = """She said "hi"
and left."""

A triple-quoted string can contain double quotes, and like the other string literals it can span several lines.

printfn "%s has %d letters" word word.Length
printfn "first letter: %c" word[0]
printfn "%s" (word + " and hawk")

The Length property gives the number of characters, an index in square brackets returns a character, and the + operator joins strings. Strings are covered in detail in F# string.

λ dotnet fsi strings.fsx
falcon has 6 letters
first letter: f
falcon and hawk
C:\Users\lucia\docs
She said "hi"
and left.

Type annotations

A type can be written explicitly after the name, separated by a colon. The annotation documents the code, and the compiler checks that the value matches it.

annotations.fsx
let count: int = 12
let weight: float = 72.5
let initial: char = 'L'
let name: string = "Lucia"
let active: bool = true

let total: int64 = 1_500L
let amount: decimal = 1_250.50m

printfn "%s %c. weighs %.1f kg" name initial weight
printfn "count = %d, active = %b" count active
printfn "total = %d, amount = %M" total amount

printfn "%s" (count.GetType().FullName)
printfn "%s" (weight.GetType().FullName)
printfn "%s" (amount.GetType().FullName)

We annotate bindings of seven types.

let total: int64 = 1_500L

An annotation does not convert between arbitrary types. The compiler only widens an int to an int64 or a float when the annotation asks for it, so 1_500 without the suffix would work as well. The other direction is a compile error: let small: int = 1_500L is rejected, because an int64 may not fit into an int.

printfn "%s" (count.GetType().FullName)

The GetType method returns the .NET type of a value. The output shows that int, float and decimal are System.Int32, System.Double and System.Decimal. How the compiler infers types is the subject of F# type inference.

λ dotnet fsi annotations.fsx
Lucia L. weighs 72.5 kg
count = 12, active = true
total = 1500, amount = 1250.50
System.Int32
System.Double
System.Decimal

Converting between types

Apart from the widening of annotated values, F# does not convert numbers for us; n + 2.5, where n is an int, is a compile error. Each type has a function of the same name that converts a value to it.

conversions.fsx
let n = 7
let x = 2.5

let sum = float n + x
let truncated = int 3.99
let rounded = int (round 3.99)

printfn "sum: %f" sum
printfn "truncated: %d" truncated
printfn "rounded: %d" rounded

let text = string 42
let parsed = int "128"
let price = decimal "4.25"

printfn "text: %s, length %d" text text.Length
printfn "parsed + 1: %d" (parsed + 1)
printfn "price * 2: %M" (price * 2m)
printfn "byte 300: %d" (byte 300)

We convert between numbers and strings.

let sum = float n + x
let truncated = int 3.99
let rounded = int (round 3.99)

The float function turns the integer into a float before the addition. Converting a float to an int drops the fractional part; the round function rounds it to the nearest whole number first.

let text = string 42
let parsed = int "128"
let price = decimal "4.25"

The string function formats a value as a string, and the numeric functions also parse strings. A string that is not a number stops the program with an error.

printfn "byte 300: %d" (byte 300)

A conversion to a smaller integer type keeps only the lowest bits: 300 becomes 44.

λ dotnet fsi conversions.fsx
sum: 9.500000
truncated: 3
rounded: 4
text: 42, length 2
parsed + 1: 129
price * 2: 8.50
byte 300: 44

Source

F# language reference: Basic types

F# language reference: Literals

F# language reference: Casting and conversions

In this article we have covered the basic F# data types: integers, floats, decimals, Booleans, characters and strings, together with literal suffixes, overflow, annotations and conversions. Values of these types are bound in F# variables and combined with F# operators.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all F# tutorials.