string OCaml - 如何将 int 转换为字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9986465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 01:26:06  来源:igfitidea点击:

OCaml - How do I convert int to string?

stringintocaml

提问by EBM

How do I convert an int to a string? Example: 1 to "1".

如何将 int 转换为字符串?示例:1 到“1”。

回答by Michael Ekstrand

Use the function string_of_int(see the documentation for Pervasives, the module containing the functions that are automatically made available in the top level namespace to al OCaml programs).

使用该函数string_of_int(请参阅Pervasives的文档,该模块包含在顶级命名空间中自动提供给所有 OCaml 程序的函数)。

回答by esope

Another solution is to use the module Printf, which allows you to choose the format of printing:

另一种解决方案是使用模块 Printf,它允许您选择打印格式:

Printf.sprintf "%d" 42

gives you "42".

给你“42”。

But you might prefer using an octal, hexadecimal, binary, etc representation. For instance,

但您可能更喜欢使用八进制、十六进制、二进制等表示。例如,

Printf.sprintf "%x" 42

gives you "2a" which is the hexadecimal representation of 42.

给你“2a”,它是 42 的十六进制表示。

Printf.sprintf "0x%x" 42

would give you "0x2a".

会给你“0x2a”。

See the Printf documentationfor more details.

有关更多详细信息,请参阅Printf 文档