根据go中的语言环境将货币/浮点数字符串解析为浮点类型 [英] Parse currency / float string to float type based on locale in go

查看:115
本文介绍了根据go中的语言环境将货币/浮点数字符串解析为浮点类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何基于已知的i18n语言环境而不是对字符串进行假设的情况下,如何将浮点字符串"解析为浮点数.

I am puzzled how to parse a "float-string" to a float, based on knowing the i18n-locale but not making presumptions about the string.

例如:德国人像我一样,写"1.234,87",而美国人写"1,234.87".在我的项目中,我确实知道我期望使用哪种语言环境,但是我不想硬编码"关于该本地语言如何编写这些内容的假设.

Example: Germans, like me, write "1.234,87" when Americans write "1,234.87". In my project I do know what locale I do expect, but I do not want to "hard code" what presumptions I have about how that local writes that stuff.

我不喜欢做正则表达式/字符串替换.

I would hate to do regexp/stringreplacements.

有没有一种通用的方式来表达类似的东西

Is there a generic way to say something like

myFloat := ParseFloatByLocale("1.234,76", "DE-DE")
// myFloat => 1234.76

strconv似乎没有此功能,x/text/language

感谢任何提示!

推荐答案

最好的选择是使用标准库的strconv.您可以制作自己的包装器和语言环境的东西.最终,我将添加更多错误检查并将其转换为它自己的程序包,但这是一个主意.如果尚未找到软件包,则很有可能必须编写自己的软件包.对于更通用的解决方案,您必须考虑所有可能的输入..在每个语言环境中对其进行规范化,并在其他人使用您的工具时强制执行这些规则...考虑到if语句和段数,这将是一个更复杂的解决方案逻辑部分.好处是您知道strconv.ParseFloat期望的输入类型.因此,您真正要做的就是获取用户输入并将其转换为编程标准 https://www.quora.com/Why-do-some-countries-use-a-period-and-others-use -逗号分隔大数,主要分为欧洲等国家和英国/美国,其中德国几乎使用了欧洲所有国家的标准.在这种假设下,由于用例降为2,因此实际上没有什么可做的.

Your best bet is the use the standard library's strconv. You can make your own wrappers and locale stuff. Eventually I'd add more error checking and turn this into it's own package, but here is an idea. If you haven't found a package yet there is a good chance you will have to write your own. For a more general solution, you'd have to think about every possible input.. normalize that per locale and enforce those rules when others are using your tools... That would be a more complex solution given the number of if statements and pieces of logic.. The good part is that you know the type of input strconv.ParseFloat expects.. So all you really have to do is take the user input and transform it to the programmatic standard http://floating-point-gui.de/formats/fp/. Given numbers are mostly universal, with the exception of commas and decimal points, there shouldn't be many use cases. You might even be able to generalize further and say there are two main formats.. https://www.quora.com/Why-do-some-countries-use-a-period-and-others-use-a-comma-to-separate-large-numbers, which is largely broken down to Europe et al and British/American, where German uses the standard almost all of Europe does. Under that assumption there isn't really much to do as the use cases comes down to 2.

package main

import (
    "fmt"
    "log"
    "strconv"
    "strings"
)

func normalizeGerman(old string) string {
    s := strings.Replace(old, ",", ".", -1)
    return strings.Replace(s, ".", "", 1)
}
func normalizeAmerican(old string) string {
    return strings.Replace(old, ",", "", -1)
}

var locale map[string]func(string) string

func init() {
    locale = make(map[string]func(string) string)
    locale["DE-DE"] = normalizeGerman
    locale["US"] = normalizeAmerican
}

func main() {
    var f, f2 float64
    var err error
    // german
    if val, ok := locale["DE-DE"]; ok {
        f, err = strconv.ParseFloat(val("1.234,87"), 64)
        if err != nil {
            log.Fatal("german fail", err)
        }
    }
    //american
    if val, ok := locale["US"]; ok {
        f2, err = strconv.ParseFloat(val("1,234.87"), 64)
        if err != nil {
            log.Fatal("american fail", err)
        }
    }

    fmt.Println(f, f2)

}

这篇关于根据go中的语言环境将货币/浮点数字符串解析为浮点类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆