首页 > 解决方案 > 您如何在 Autohotkey(AHK) 中转换或缩写数字?(发布由 autohotkey 不和谐组做出的答案)

问题描述

我需要在 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

安德烈亚斯@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 今天早上帮助我。

标签: autohotkey

解决方案


安德烈亚斯@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, num, 0, 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, Exception("Illegal type", -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, Round(Floor(int / (10 ** (size * 3 - 1)))/ 10, 1) . ending
        
    }

}

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

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

您可以将行中的下一个字符添加到 EndChars


推荐阅读