fcvt,函式名,把一個浮點數轉換為字元串。
基本介紹
- 外文名:fcvt
- 解釋:函式名
- 性質:計算機
- 類別:編程
簡介,範例,示例代碼:,運行結果:,程式例,
簡介
用 法: char *fcvt(double value, int ndigit, int *decpt, int *sign);
參數:
value:要轉換的浮點數,輸入參數
ndigit:取小數的位數,輸入參數
decpt:表示小數點的位置,輸出參數
sign:表示value的符號,0為正數,1為負數,輸出參數
範例
示例代碼:
#include "stdio.h"#include "stdlib.h"int main(int argc, char const *argv[]){ double a = 1234.567111111; double b = -12345.123455; char *ptr; int decpt, sign; ptr = fcvt(a, 5, &decpt, &sign); printf("a's decpt: %d\n", decpt); printf("a value=%c%s\n", sign==0?' ':'-', ptr); ptr = fcvt(b, 6, &decpt, &sign); printf("a's decpt: %d\n", decpt); printf("b value=%c%s\n", sign==0?' ':'-', ptr); return 0;}
運行結果:
a's decpt: 4a value= 123456711a's decpt: 5b value=-12345123455
程式例
#include <stdlib.h>#include <stdio.h>#include <conio.h>int main(void){ char *string; double value; int dec, sign; int ndig = 10; value = 9.876; string =fcvt(value, ndig, &dec, &sign); printf("string = %s dec = %d \ sign = %d\n", string, dec, sign); value = -123.45; ndig= 15; string =fcvt(value,ndig,&dec,&sign); printf("string = %s dec = %d sign = %d\n", string, dec, sign); value = 0.6789e5; /* scientific notation */ ndig = 5; string =fcvt(value,ndig,&dec,&sign); printf("string = %s dec = %d\ sign = %d\n", string, dec, sign); return 0;}