我有cmd prompt,其中stdin只接受字符串,但是一旦我收到字符串就需要convert to float。當(dāng)有人錯(cuò)誤輸入時(shí),"0..1" instead of 0.1,我需要檢查并顯示錯(cuò)誤信息。 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 } }檢查字符串值中是否有多個(gè)點(diǎn),如果存在多個(gè)點(diǎn),則拋出錯(cuò)誤
1 回答

斯蒂芬大帝
TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超8個(gè)贊
如果您只是檢查數(shù)字,請(qǐng)使用此選項(xiàng)而不是點(diǎn)檢查
i, err := strconv.ParseFloat(elem, 64)
if err != nil {
numbers = append(numbers, i)
}
- 1 回答
- 0 關(guān)注
- 185 瀏覽
添加回答
舉報(bào)
0/150
提交
取消