简体   繁体   English

如何在“ C”中保留小数点后抑制或消除零

[英]how to supress or eliminate zeros after a decimal in 'C'

My program is below, this is for class. 我的程序在下面,这是上课的。 My question is what should I be adding to my program to either stop or eliminate the '.' 我的问题是我应该在程序中添加什么以停止或消除“。”。 decimals and zeros trailing after my answer is printed. 我的答案打印出来后的小数和零。

// Ch5Pgm7.cpp : Takes a number entered by the user and cubes it.
//Written by: Chris Howard       Sept. 25th 2014.

#include "stdafx.h"
#include "stdio.h"

void cube(double NUM);      // My function that will cube the users input

int main(void)
{
double NUM;
printf("Please enter a number: ");
scanf_s("%lf", &NUM);
cube(NUM);

return 0;
}

void cube(double NUM)
{
NUM = NUM * NUM * NUM;

printf("%lf\n", NUM);
}

Use . 使用. and the number of decimal spaces you want. 以及所需的小数位数。

printf("%.0lf\n", NUM);

假设您要坚持浮点运算,并且不想使用整数,则可以使用g转换说明符:

printf("%g\n", NUM);

you can use the g specifier to get rid of the decimal . 您可以使用g指示符除去小数点. and the trailing zeroes 和尾随零

printf("%.0g", NUM);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM