1

我有这个代码:

            void FeetInches::decimal() 
            {
                if (inches == 6.0)
                {
                    inches = 5;
                    std::cout << feet << "." << inches << " feet";  //not the best but works..
                }
            }

这会将 12 英尺 6 英寸打印为 12.5 英尺。我宁愿不使用这种“hackish”方法,而是让它像这样:

            void FeetInches::decimal() 
            {
                if (inches == 6.0)
                {
                    inches = .5;
                    std::cout << feet << inches << " feet";  //not the best but works..
                }
            }

但这将打印 60.5 英寸(我需要 6.5 英寸)。基本上,如果我单独打印英寸,它会打印 0.5。我希望英寸只打印 0.5 而没有零。不能使用 printf 方法或其他快速技术来实现这一点吗?顺便说一下,数据类型是双重的

4

1 回答 1

8

先把你的英寸换算成英尺怎么样:

feet = feet + inches / 12.0;

现在打印出结果。或者,如果您不想更改feet变量,请直接在cout语句中进行计算或使用临时变量进行计算。

于 2012-10-12T21:18:58.773 回答