为什么我得到的数据预警“双'到'浮动',可能损失转换?(C) [英] Why do I get a conversion from 'double' to 'float', possible loss of data warning?(c)

查看:158
本文介绍了为什么我得到的数据预警“双'到'浮动',可能损失转换?(C)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个程序,它基本上是计算对进口车的责任。假设用户输入的值的轿厢和装运价格的值。但是,当我计算我得到这个错误,是不好的。

I'm writing a program which basically calculates the duty on a car imported. The user is supposed to input the values the values for the price of the car and the shipment. But when I calculate I get this error which is not good.

//总值班要收取进口//汽车

//TOTAL DUTY TO BE CHARGED ON A CAR IMPORTED//

main()
{

        float purchaseprice, shipmentcosts, ecowaslevy, importlevy, GCNETlevy, TotalDuty;

        printf("Enter the purchase price and shipment cost");


        scanf("%f,%f", &purchaseprice, &shipmentcosts);
        ecowaslevy = 0.01f, importlevy = 0.25f, GCNETlevy = 0.05f;
        //FORMULAS FOR THE LEVY//
        ecowaslevy = (purchaseprice + shipmentcosts) * (0.01); 
        importlevy = (purchaseprice + shipmentcosts) * (0.25); 
        GCNETlevy =  (purchaseprice + shipmentcosts) * (0.05); 
        //FORMULA FOR TOTAL DUTY//
        TotalDuty = (ecowaslevy + importlevy + GCNETlevy)*(3.2);
        printf("TOTAL DUTY=GHC.%f", TotalDuty);
        system("pause");

}

这是警告:

1> C:\\用户\\大卫\\文档\\的Visual Studio 2013 \\项目\\总费用\\总费用\\ tot.c(9):警告C4244:'=':从转换'双'到'浮动',可能丢失数据

1>c:\users\david\documents\visual studio 2013\projects\total expenses\total expenses\tot.c(9): warning C4244: '=' : conversion from 'double' to 'float', possible loss of data

1> C:\\用户\\大卫\\文档\\的Visual Studio 2013 \\项目\\总费用\\总费用\\ tot.c(10):警告C4244:'=':由'双'到'浮动'的转换,可能丢失数据

1>c:\users\david\documents\visual studio 2013\projects\total expenses\total expenses\tot.c(10): warning C4244: '=' : conversion from 'double' to 'float', possible loss of data

1> C:\\用户\\大卫\\文档\\的Visual Studio 2013 \\项目\\总费用\\总费用\\ tot.c(11):警告C4244:'=':由'双'到'浮动'的转换,可能丢失数据

1>c:\users\david\documents\visual studio 2013\projects\total expenses\total expenses\tot.c(11): warning C4244: '=' : conversion from 'double' to 'float', possible loss of data

推荐答案

在几行您常数值乘以像 <0.01 / code>和 0.25 。这些值是双打和乘法的结果是双,你再有较少precision浮点型变量存储。这给你一个编译器警告。要解决它,你可以在˚F字面后缀附加到常数值,例如 0.01F 0.25f 等。

In several lines you multiply by constant values like 0.01 and 0.25. These values are doubles and the result of the multiplication is double which you then store in a float variable having less precision. This gives you a compiler warning. To fix it you can attach the f literal suffix to the constant values, e.g. 0.01f and 0.25f etc.

这篇关于为什么我得到的数据预警“双'到'浮动',可能损失转换?(C)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆