Multiplying float values “possible lossy conversion from double to float”

前端 未结 3 476
广开言路
广开言路 2020-12-12 05:25

I have this problem wherein I have to convert kilometers into miles. I\'m a novice programmer so bear with me.

Here\'s my code so far:

import java.ut         


        
3条回答
  •  半阙折子戏
    2020-12-12 05:58

    The value 0.621371 is a double literal, so the km values is promoted to double when multiplied. Storing the double product back to m would be a conversion that could lose data (double to float).

    To keep the data as a float, use a float literal, with a f on the end:

    m=km*0.621371f;
    

    Normally a double for the results would be just fine, so you could also just change the datatypes of m and km to double.

提交回复
热议问题