Converting between numeric types

Category:
Arithmetic
Description: This sample shows how to convert between various numeric types
Code:
let SampleArithmetic3() =
// Manipulating double-precision (64-bit) floating point numbers
let pi1 = float 3 + 0.1415 // 'float' is an overloaded conversion operator
let pi2 = double 3 + 0.1415 // identical - 'double' is a synonym for 'float'
printfn "pi1 = %f, pi2 = %f" pi1 pi2

let i1 = int 3.1415
let i2 = int64 3.1415
printfn "i1 = %d, i2 = %d" i1 i2

// Manipulating single-precision (32-bit) floating point numbers
let f32a = 2.1415f + 1.0f // float32 (System.Single)
let f32b = 2.1415f + float32 1 // float32 - identical
printfn "f32a = %f, f32b = %G" f32a f32b

// Manipulating bytes
let byteA = byte (3+4) // byte
let byteB = 255uy // byte
let byteC = 0xFFuy // byte
let byteD = byte 0xFF // byte
printfn "byteA = %d, byteB = %d" byteA byteB

Execution Result:
pi1 = 3.141500, pi2 = 3.141500
i1 = 3, i2 = 3
f32a = 3.141500, f32b = 3.1415
byteA = 7, byteB = 255

Last edited Sep 14, 2011 at 2:22 AM by ttliu2000, version 1

Comments

No comments yet.