我有cmd prompt,其中stdin只接受字符串,但是一旦我收到字符串就需要convert to float。当有人错误输入时,"0..1" instead of 0.1,我需要检查并显示错误信息。 msg := "enter the rate eg:{0.1}" rate, err := RatePrompt(msg) if err != nil { fmt.Println("something went while entering rate, please re-enter") } func RatePrompt(cmrmsg string) (price string, err error) { fmt.Println(" ") scanner := bufio.NewScanner(os.Stdin) scanner.Scan() price := scanner.Text() return price, nil } //check for string contains multiple dot rate, err := RatePrompt(msg) if err != nil { fmt.Println("something went while entering rate, please re-enter")else{ ///check for string contains multiple dot } }检查字符串值中是否有多个点,如果存在多个点,则抛出错误
1 回答
斯蒂芬大帝
TA贡献1827条经验 获得超8个赞
如果您只是检查数字,请使用此选项而不是点检查
i, err := strconv.ParseFloat(elem, 64)
if err != nil {
numbers = append(numbers, i)
}
- 1 回答
- 0 关注
- 121 浏览
添加回答
举报
0/150
提交
取消