微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

您如何在AutohotkeyAHK中转换或缩写数字? 发布自动热键不和谐小组做出的答案

如何解决您如何在AutohotkeyAHK中转换或缩写数字? 发布自动热键不和谐小组做出的答案

我需要为正在制作的游戏在Autohotkey中转换数字,而Autohotkey discord Group的一些成员能够帮助我。 特别是vieira和andreas @ Nasa:〜$ sudo -i都提出了一个可行的解决方案。

维埃拉

print(GetFormatednum(1234567890))
print(GetFormatednum(1234567))
print(GetFormatednum(1234))
print(GetFormatednum(12))

GetFormatednum(num) {
    for k,v in [{12:"T"},{9:"B"},{6:"M"},{3:"K"}] {
        for i,j in v
            if (num >= (10**i))
                return % SubStr(num/(10**i),1,5) j
    }
    return num
}
1.234B
1.234M
1.234K
12

andreas @ Nasa:〜$ sudo -i

InputBox,num,Num Input,Input the number you want to be converted
if num is not Integer
    return
MsgBox,% "Num is: " . num

MsgBox,% "this is converted: " . Converter.Convert(num)

return


class Converter {

    static 1 := "k"
    static 2 := "M"
    static 3 := "G"
    static 4 := "T"

    Convert(int){
        if int is not Integer
            Throw,Exception("Illegal type",-1)
        size := Floor(Floor(Log(int)) / 3)
        
        if(size == 0)
            return int

        While(true){
            Try {
                ending := this[size]
                break
            }
            Catch e {
                if(e.Message == "key to great")
                    size--
            }
        }

        return,Round(Floor(int / (10 ** (size * 3 - 1)))/ 10,1) . Ending
        
    }

    __Get(vKey){
        if(vKey > 0)
            Throw,Exception("key to great")
        return,0
    }

}

我非常感谢他们每个人以及今天早上BoBo和elmodo7对我的帮助。

解决方法

andreas @ Nasa:〜$ sudo -i

InputBox,num,Num Input,Input the number you want to be converted
if num is not Integer
    return
MsgBox,% "Num is: " . num

MsgBox,% "this is converted: " . Converter.Convert(num)

return


class Converter {

    static 1 := "k"
    static 2 := "M"
    static 3 := "B"
    static 4 := "T"
    Convert(int){
        if int is not Integer
            Throw,Exception("Illegal type",-1)
        size := Floor(Floor(Log(int)) / 3)
        
        if(size == 0)
            return int

        While(true){
            Try {
                ending := this[size]
                break
            }
            Catch e {
                if(e.Message == "key to great")
                    size--
            }
        }

        return,Round(Floor(int / (10 ** (size * 3 - 1)))/ 10,1) . Ending
        
    }

    __Get(vKey){
        if(vKey > 0)
            Throw,Exception("key to great")
        return,0
    }

}

编辑:就个人而言,这一切超出了我,但这是他的最终答案。

conv := new Converter()

Loop,10 {
    Random,2147483647
    num := num * 1000000
    Print("Num is: " . num . " and this is converted: " . conv.Convert(num))
}

return


class Converter {

    __New(){
        this.endingChars := new EndChars("k","M","G","T") ; put the endings for each step here in the correct order...
    }

    Convert(int){
        if int is not Integer
            Throw,-1)
        size := Floor(Floor(Log(int)) / 3)
        
        if(size == 0)
            return int

        While(size > 0){
            Try {
                ending := this.endingChars[size]
                break
            }
            Catch e {
                size--
            }
        }

        return,1) . ending
        
    }

}

class EndChars {
        
    __New(EndingChars*){
        for k,i in EndingChars
            this[k] := i
    }

    __Get(vKey){
        if(vKey > 0)
            Throw,0
       }
    
}

,您只需将下一个字符添加到EndChars

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。