Why does division result in zero instead of a decimal?

前端 未结 5 627
隐瞒了意图╮
隐瞒了意图╮ 2020-11-21 05:38

Teaching myself C and finding that when I do an equation for a temp conversion it won\'t work unless I change the fraction to a decimal. ie,

tempC=(.555*(tempF

5条回答
  •  感动是毒
    2020-11-21 06:20

    It looks like you have integer division in the second case:

    tempC=((5/9)*(tempF-32))
    

    The 5 / 9 will get truncated to zero.

    To fix that, you need to make one of them a floating-point type:

    tempC=((5./9.)*(tempF-32))
    

提交回复
热议问题