浮点数字符串转换成浮点数实现

504 篇文章 0 订阅

转自: http://blog.csdn.net/magictong/article/details/6900410


/* -------------------------------------------------------------------------
//	文件名		:	StringToFloat.cpp
//	创建者		:	magictong
//	创建时间	:	2011-9-6 14:14:02
//	功能描述	:	
//
//	$Id: $
// -----------------------------------------------------------------------*/

#include "stdafx.h"
#include "StringToFloat.h"

// -------------------------------------------------------------------------

#pragma warning(disable:4244)
// -------------------------------------------------------------------------
// 函数		: StrToFloatA
// 功能		: 将一个字符串转换为浮点数
// 返回值	: float 
// 参数		: char* pstrfloat
// 附注		: 
// -------------------------------------------------------------------------
float StrToFloatA(char* pstrfloat)
{
	// check
	if (!pstrfloat)
	{
		return 0.0;
	}

	bool bNegative = false;
	bool bDec = false;

	char* pSor = 0;
	char chByte = '0';
	float fInteger = 0.0;
	float fDecimal = 0.0;
	float fDecPower = 0.1f;

	// 进行首位判断,判断是否是负数
	if (pstrfloat[0] == '-')
	{
		bNegative = true;
		pSor = pstrfloat + 1;
	}
	else
	{
		bNegative = false;
		pSor = pstrfloat;
	}

	while (*pSor != '\0')
	{
		chByte = *pSor;

		if (bDec)
		{
			// 小数
			if (chByte >= '0' && chByte <= '9')
			{
				fDecimal += (chByte - '0') * fDecPower;
				fDecPower = fDecPower * 0.1;
			}
			else
			{
				return (bNegative? -(fInteger +  fDecimal): fInteger + fDecimal);
			}
		}
		else
		{
			// 整数
			if (chByte >= '0' && chByte <= '9')
			{
				fInteger = fInteger * 10.0 + chByte - '0';
			}
			else if (chByte == '.')
			{
				bDec = true;
			}
			else
			{
				return (bNegative? -fInteger : fInteger);
			}
		}

		pSor++;
	}

	return (bNegative? -(fInteger +  fDecimal): fInteger + fDecimal);
}

// -------------------------------------------------------------------------
// 函数		: StrToFloatW
// 功能		: 将一个字符串转换为浮点数
// 返回值	: float 
// 参数		: char* pstrfloat
// 附注		: 
// -------------------------------------------------------------------------
float StrToFloatW(wchar_t* pstrfloat)
{
	// check
	if (!pstrfloat)
	{
		return 0.0;
	}

	bool bNegative = false;
	bool bDec = false;

	wchar_t* pSor = 0;
	wchar_t chByte = L'0';
	float fInteger = 0.0;
	float fDecimal = 0.0;
	float fDecPower = 0.1f;

	// 进行首位判断,判断是否是负数
	if (pstrfloat[0] == L'-')
	{
		bNegative = true;
		pSor = pstrfloat + 1;
	}
	else
	{
		bNegative = false;
		pSor = pstrfloat;
	}

	while (*pSor != L'\0')
	{
		chByte = *pSor;

		if (bDec)
		{
			// 小数
			if (chByte >= L'0' && chByte <= L'9')
			{
				fDecimal += (chByte - L'0') * fDecPower;
				fDecPower = fDecPower * 0.1;
			}
			else
			{
				return (bNegative? -(fInteger +  fDecimal): fInteger + fDecimal);
			}
		}
		else
		{
			// 整数
			if (chByte >= L'0' && chByte <= L'9')
			{
				fInteger = fInteger * 10.0 + chByte - L'0';
			}
			else if (chByte == L'.')
			{
				bDec = true;
			}
			else
			{
				return (bNegative? -fInteger : fInteger);
			}
		}

		pSor++;
	}

	return (bNegative? -(fInteger +  fDecimal): fInteger + fDecimal);
}
// -------------------------------------------------------------------------
// $Log: $


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值