duan_2000 2019-06-15 14:51
浏览 73
已采纳

将两个值相除得到0

I'm trying to make a division of two variables, but when I want to print the result, the program prints out 0

the leadTime and endAmount are being correctly printed by te program, but the monthlyAmount is being printed as 0. Also if I remove the float64() around endAmount and leadTime it is being printed as 0

var leadTime int
if currentAge < 45 {
    leadTime  = 120
} else if currentAge > 45 && currentAge < 55 {
    leadTime = 90
} else if currentAge > 55 {
    leadTime = 60
}
endAmount, _ := strconv.Atoi(amountAsString)

monthlyAmount:= (float64(endAmount) / float64(leadTime)
fmt.Println("leadTime :", leadTime )
fmt.Println("Total amount:", endAmount)
fmt.Println("monthlyAmount:", monthlyAmount)

with the standardinput I'm testing with, the leadTime = 120 and the endAmount = 93735.00 so the monthlyAmount should be: 781.13

  • 写回答

1条回答 默认 最新

  • douao1579 2019-06-15 15:22
    关注

    The problem is your trying to convert floating string an int in

    endAmount, _ := strconv.Atoi(amountAsString)
    

    This will give an error, but you're just neglecting it. Use strconv.ParseFloat(amountAsString, 64) to solve your problem.

    Check the playground example to see the error and the working code

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥30 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题