|
发表于 2020-7-12 09:30:01
|
显示全部楼层
mark,以前用C++写过一个数字转英文大写的,不知有帮助没,代码如下:
//EnglishNumber.h
#pragma once
#include<string>
#include<cassert>
#include "ValueTraits.h"
using namespace std;
class EnglishNumber
{
public:
EnglishNumber(){}
~EnglishNumber(){}
template<typename ValueType>
const string NumberName(const ValueType &) const;
private:
template<typename ValueType>
const string BaseDigit(const ValueType &) const;
template<typename ValueType>
const string TensDigit(const ValueType &) const;
template<typename ValueType>
const string HundredsDigit(const ValueType &) const;
template<typename ValueType>
const string MoreDigit(const ValueType &) const;
};
template<typename ValueType>
const string EnglishNumber::BaseDigit(const ValueType & iNumber) const{
const static string BaseNumName[] = {"zero",
"one","two","three","four","five","six",
"seven","eight","nine","ten","eleven",
"twelve","thirteen","fourteen","fifteen",
"sixteen","seventeen","eighteen","nineteen"};
assert(iNumber >= 0 && iNumber < 20 && "Function BaseNumber");
return BaseNumName[ValueTraits<ValueType>::Long(iNumber)];
}
template<typename ValueType>
const string EnglishNumber::TensDigit(const ValueType & iNumber) const{
const static string TensDigitName[] = {
"twenty","thirty","forty","fifty",
"sixty","seventy","eighty","ninety"};
assert(iNumber >= 0 && iNumber < 100 && "Function TensDigit");
if(iNumber < 20)
return BaseDigit(iNumber);
ValueType h(iNumber),l(iNumber);
h = iNumber / 10;
l = iNumber % 10;
if(0 == l)
return TensDigitName[ValueTraits<ValueType>::Long(h) - 2];
return TensDigitName[ValueTraits<ValueType>::Long(h) - 2] + "-" + BaseDigit(l);
}
template<typename ValueType>
const string EnglishNumber::HundredsDigit(const ValueType & iNumber) const{
assert(iNumber >= 0 && iNumber < 1000 && "Function HundredsDigit");
if(iNumber < 100)
return TensDigit(iNumber);
ValueType h,l;
h = iNumber / 100;
l = iNumber % 100;
if(0 == l)
return BaseDigit(h) + " hundred";
return BaseDigit(h) + " hundred and " + TensDigit(l);
}
template<typename ValueType>
const string EnglishNumber::MoreDigit(const ValueType & iNumber) const{
const static string MoreName[] = {"thousand","million","billion"};
assert(iNumber >= 0 && "Function MoreNumber");
if(iNumber < 1000)
return HundredsDigit(iNumber);
ValueType n;
n = iNumber % 1000;
string result;
if(0 != n)
result = HundredsDigit(n);
n = iNumber / 1000;
int i = 0;
do{
ValueType l = n % 1000;
n /= 1000;
if(0 != l)
if(0 == result.size())
result = HundredsDigit(l) + " " + MoreName[i];
else
result = HundredsDigit(l) + " " + MoreName[i] + ", " + result;
}while(n > 0 && ++i < 3);
if(n > 0)
if(0 == result.size())
result = MoreDigit(n) + " trillion";
else
result = MoreDigit(n) + " trillion, " + result;
return result;
}
template<typename ValueType>
const string EnglishNumber::NumberName(const ValueType & iNumber) const{
if(iNumber < 0)
return "minus " + MoreDigit(-iNumber);
return MoreDigit(iNumber);
}
//ValueTraits.h
#pragma once
template<typename T>struct ValueTraits
{
static long Long(const T & v)
{
return v;
}
};
|
|