// Q766008.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Q766008.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
CWinApp theApp;
using namespace std;
void conv1(double d, int* a, int *b)
{
// int id = (int)(d * 100 + 0.5);如果要四舍五入,用这个
int id = (int)(d * 100);
*a = id / 100;
*b = id % 100;
}
void conv(double d, char * buffer)
{
int x;
int y;
conv1(d, &x, &y);
if (x == 0 && y == 0)
{
sprintf(buffer, "0");
}
else if (y == 0)
{
sprintf(buffer, "%d", x);
}
else if (y < 10)
{
sprintf(buffer, "%d.0%d", x, y);
}
else
{
if (y % 10 != 0)
sprintf(buffer, "%d.%d", x, y);
else
sprintf(buffer, "%d.%d", x, y / 10);
}
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
//CString strHello;
//strHello.LoadString(IDS_HELLO);
//cout << (LPCTSTR)strHello << endl;
double arr[] = { 0.00077777, 0.200000, 2.00000, 2.55555, 1000.66666, 1999.00000 };
for (int i = 0; i < 6; i++)
{
char buffer[100];
conv(arr[i], buffer);
CString str = (CString)buffer;
cout << (LPCTSTR)str << endl;
}
}
return nRetCode;
}
0
0.2
2
2.55
1000.66
1999
Press any key to continue
本代码的限制:
转换的值必须是>=0
转换的值必须小于2100万。
可以保证在以上条件下满足你的需求