Skip to content

【Move语法问题】如何将一个u64整型转为字符串? #234

Answered by qiwihui
xiegudong45 asked this question in Q&A
Discussion options

You must be logged in to vote

补充: u64 版本

    use std::string::{Self, String};
    use std::vector;

    /// @dev Converts a `u64` to its `ascii::String` decimal representation.
    public fun to_string(value: u64): String {
        if (value == 0) {
            return string::utf8(b"0")
        };
        let buffer = vector::empty<u8>();
        while (value != 0) {
            vector::push_back(&mut buffer, ((48 + value % 10) as u8));
            value = value / 10;
        };
        vector::reverse(&mut buffer);
        string::utf8(buffer)
    }

    /// @dev Converts a `ascii::String` to its `u64` decimal representation.
    public fun to_integer(s: String): u64 {
        let res = 0;
        let s_bytes = *string

Replies: 3 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@MagicGordon
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by xiegudong45
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants