OCaml - How do I convert string to int?

17,079

Solution 1

You could use the 'int_of_string' function described in the documentation.

Solution 2

I would start with int_of_string. Generally, OCaml standard library provides functions, that converts between types, of the following form <output>_of_<input>, e.g., float_of_string, string_of_int, etc.

Share:
17,079
Teshtek
Author by

Teshtek

Updated on June 04, 2022

Comments

  • Teshtek
    Teshtek almost 2 years

    Im' newbie in Ocaml and Im'trying to do this :

    let medio a b =
        (a + b);;
    let () = Printf.printf "%d + %d  = %d\n" Sys.argv.(1) Sys.argv.(2) (medio Sys.argv.(1) Sys.argv.(2))    
    

    Sys.argv.(1) has to be the arg[1] ~ in C

    Now I want to use them like parameters for my function medio, but they 're strings. How can I parase them into int ? Is there a ocaml function to do it? In python is int(Sys.argv.(2)) or int atoi(const char *str) in C in ocaml ?