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

将两个值相除得到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 本地测试网站127.0.0.1 已拒绝连接,如何解决?(标签-ubuntu)
  • ¥50 Qt在release捕获异常并跟踪堆栈(有Demo,跑一下环境再回答)
  • ¥30 python,LLM 文本提炼
  • ¥15 关于将inet引入的相关问题
  • ¥15 关于一个倒计时的操作和显示设计
  • ¥15 提问STK的问题,哪位航天领域的同学会啊
  • ¥15 苹果系统的mac m1芯片的笔记本使用ce修改器使用不了
  • ¥15 单相逆变的电压电流双闭环中进行低通滤波PID算法改进
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 如何卸载arcgis 10.1 data reviewer for desktop