您的位置:首页 > 编程语言 > C语言/C++

C++ 的string 与java的String中的汉字长度

2010-10-29 17:14 190 查看
Code:

public class Example

{

public Example(double x)

{

String yuan="亿千百拾万千百十元角分";

String digit="零壹贰叁肆伍陆柒捌玖";

String str="";

int y=(int)Math.round(x*100-0.5);//123414

int i=yuan.length()-1;

while(y>0&&i>0)

{

str=""+digit.charAt(y%10)+yuan.charAt(i)+str;

i--;

y=y/10;

}

System.out.println(str); //壹千贰百叁十肆元壹角肆分

}

public static void main(String[] args)

{

new Example(1234.14);

}

}

Code:

#include "stdafx.h"

#include <iostream>

#include <cstring>

#include <string>

#include <algorithm>

#include <vector>

using namespace std;

class Money

{

public:

string M(string original)

{

static string yuan="亿千百拾万千百十元角分";

static string digit="零壹贰叁肆伍陆柒捌玖";

long double b;

b=strtod(&original[0],NULL);

int n=int(100*b);//123414

int v=0;

int i=yuan.length()-1;//21

string str="";

while(i>0&&n>0)

{

v=n%10;

str=yuan[i]+str;

str=yuan[i-1]+str;

str=digit[2*v+1]+str;

str=digit[2*v]+str;

i=i-2;

n=n/10;

}

return str;

};

};

int _tmain(int argc, _TCHAR* argv[])

{

Money T;

cout<<T.M("1234.14");

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐