F# - Convert decimal places to numbers - Asked By Dom Afonso on 15-Jun-12 10:43 AM

I want a method or function that can divide, take decimal places, convert them to numbers and distribute them to numbers without decimal places. The function or method could return something like this:
5:2 =2.5 and output 2 and 3 where 3 is 2+1 and this one is the convertion of 0.5 decimal place.
Venkat K replied to Dom Afonso on 15-Jun-12 10:45 PM
Though i'm not aware of F# syntaxes i'm trying to give a psuedo code in c#

string ConvertToNumbers(stringexpression)
{
  var expSplit = stringexpression.split(":").ToArrayList();
    string  divexp = expSplit[0]/expSplit[1];
    int firstnum = Math.Ceiling(divexp) - 1;
    int secondnum = Math.Ceiling(divexp);
  return firstnum + "," + secondnum;
}


Vikram Singh Saini replied to Dom Afonso on 16-Jun-12 09:08 AM
As per I have understood your requirement, you want to divide two values (for e.g. 5 by 2), and then want to roundoff them to nearest integer number.

For same code below in F#:

//using System

open System

 

// Function for dividing

let divide x y =

   x / y

 

// _ is main function in F#

let _ =

    let result = divide 5M 2M

    let roundOff = ceil result

    Console.WriteLine roundOff


Understanding the code:

(1) open System is equivalent to using System in C#
(2) Declared function named divide that accepts two arguments x and y, and then return result of type decimal. This is calculated automatically by F# compiler.
(3) Third we have called the divide function in main function _ , rounded off the result by using ceil method of Math class. And at last showed the result to user by Console.WriteLine.

Note:
Select the entire code in VS IDE and then right click --> select Send to Interactive (Alt + Enter)

Hope this helps.
bharti odedra replied to Dom Afonso on 18-Jun-12 07:10 AM
try this

open System

 

let divide x y =

   x / y

 

let _ =

    let result = divide 5M 2M

    let roundOff = ceil result

    Console.WriteLine roundOff