Home

F# Example: Percentage Conversion

   

Introduction

This example converts decimal values from one category such as fraction or percentage to another category.

let convertPercentageFromDecimal percent value =
    percent * value / 100.00;

let result1 = convertPercentageFromDecimal 8.75 1500.00;
printfn "Conversion: %.2f" result1;
// ------------------------------------------------
let convertFractionToPercent numerator denominator =
    if denominator <> 0.00 then
        (numerator / denominator) * 100.00;
    else
        0.00

let result2 = convertFractionToPercent 3.00 4.00;
printfn "Conversion: %.2f" result2;
// ------------------------------------------------
let convertDecimalToPercent value =
    value * 100.00

let result3 = convertDecimalToPercent 0.125;
printfn "Conversion: %.2f%s" result3 "%";
// ------------------------------------------------
let convertPercentToDecimal value =
    value / 100.00

let result4 = convertPercentToDecimal 14.85;
printfn "Conversion: %.2f%%" result4; 
// ------------------------------------------------

This would produce:

Conversion: 131.25
Conversion: 75.00
Conversion: 12.50%
Conversion: 0.15%
Press any key to continue . . .
 
   
     
 

Home Copyright © 2015 FunctionX Home