2014-09-25 67 views
1

我的程序在下面,這是爲了課程。 我的問題是我應該添加到我的程序,以停止或消除'。'小數點後 和零後打印我的答案。如何在'C'後小數點以後抑制或消除零點

// 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); 
} 
+2

閱讀上printf的格式化字符串:http://linux.die.net/man/3/printf – datenwolf 2014-09-25 18:36:15

+0

^^迄今爲止的最佳答案。 – 2014-09-25 18:38:25

回答

0

假設你要堅持浮點和不想使用整數,你可以使用g轉換說明:

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

@datenwold我的理解是他想將'42.1'作爲'42.1'和'42.0'作爲'42'來打印。 – ouah 2014-09-25 18:39:15

+0

非常感謝你!像魅力一樣工作。 – ChrisHoward 2014-09-25 18:39:18

+0

@ouah不要介意 – datenwolf 2014-09-25 18:40:16

1

使用.,你想小數位的數量。

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

您可以用G符擺脫小數.和尾隨零

printf("%.0g", NUM); 
相關問題