小数位数的保留代码与大小写转换
2014-01-08 08:50
190 查看
小数位数的保留代码
最近写了两个小工具,要将小数部分四舍五入至一位小数和整数,可函数中round()是所谓的银行家进位法,实在用不了。无奈找度娘询问了一阵,结果不尽人情呀,最后找到一个保留两位小数的代码,通过修改后令人满意。现分享大家留着备用:
============================
保留1位小数
===========================
function BL1(s: real): real;
var
r1, r2: real;
s1, s2: string;
begin
r1 := int(s);
r2 := frac(s);
s1 := copy(floattostr(r1), 1, length(floattostr(r1)));
if length(floattostr(r2)) >= 4 then
begin
if strtoint(copy((floattostr(r2)), 4, 1)) >= 5 then //小数第三位开始复制一位0.00
if strtoint(copy((floattostr(r2)), 3, 1)) = 9 then
begin
s1 := inttostr(strtoint(s1) + 1);
s2 := '';
end
else
S2 := inttostr(strtoint(copy((floattostr(r2)), 3, 1)) + 1)
else if copy((floattostr(r2)), 3, 1) = '0' then
s2:=''
else s2 := copy(floattostr(r2), 3, 1);
end
else s2 := copy(floattostr(r2), 3, 1);
result := strtofloat(s1 + '.' + s2);
end;
====================
调用
=====================
label3.Caption :=floattostr(BL(strtofloat(edit1.text)));
================================
四舍五入到整数
================================
function BL0(s: real): real;
var
r1, r2: real;
s1, s2: string;
begin
r1 := int(s); //取整数部分
r2 := frac(s); //取小数部分
s1 := copy(floattostr(r1), 1, length(floattostr(r1))); //复制R1,复制位数为R1长度
if length(floattostr(r2)) >= 3 then //如果小数部分长度>=3
if strtoint(copy((floattostr(r2)), 3, 1)) >= 5 then //从小数部分第三位开始复制一位数
s1 := inttostr(strtoint(s1) + 1); //如果第一位小数大于5就向整数个位进1
result := strtofloat(s1);
end;
========================================================
四舍五入保留两位代码
========================================================
function BL2(s: real): real;
var
r1, r2: real;
s1, s2: string;
begin
r1 := int(s);
r2 := frac(s);
s1 := copy(floattostr(r1), 1, length(floattostr(r1)));
if length(floattostr(r2)) >= 5 then
begin
if strtoint(copy((floattostr(r2)), 5, 1)) >= 5 then
if strtoint(copy((floattostr(r2)), 4, 1)) = 9 then
if strtoint(copy((floattostr(r2)), 3, 1)) = 9 then
begin
s1 := inttostr(strtoint(s1) + 1);
s2 := '';
end
else
S2 := inttostr(strtoint(copy((floattostr(r2)), 3, 1)) + 1)
else if copy((floattostr(r2)), 3, 1) = '0' then
S2 := '0' + inttostr(strtoint(copy(floattostr(r2), 3, 2)) + 1)
else s2 := inttostr(strtoint(copy(floattostr(r2), 3, 2)) + 1)
else s2 := copy(floattostr(r2), 3, 2);
end
else s2 := copy(floattostr(r2), 3, 2);
result := strtofloat(s1 + '.' + s2);
end;
================================================
大小写转换
================================================
function TForm1.ConvertMoney(Num: Real): String;
var
intstr,decstr,s: String;
intlen,declen,i: word;
begin
Intstr:= intToStr(Trunc(Num));
decstr := FloatToStr(RoundTo(Frac(num),-1));//对小数进行四舍五入
//decstr := FloatToStr(RoundTo(Frac(num),-2));//对小数进行四舍五入
decstr := copy(decstr,3,Length(decstr)-1);
declen := Length(decstr);
intlen := Length(Intstr);
For i := 1 to Intlen do
begin
Case StrToInt(Intstr[i]) of
0: begin
if (copy(s,Length(s)-1,2)<>'零') then
s := s+'零';
end;
1: s := s+'壹';
2: s := s+'贰';
3: s := s+'叁';
4: s := s+'肆';
5: s := s+'伍';
6: s := s+'陆';
7: s := s+'柒';
8: s := s+'捌';
9: s := s+'玖';
end;
case intlen-i+1 of
13: begin
if (StrToInt(Intstr[i])<>0)then
s := s+'万';
end;
12: begin
if (StrToInt(Intstr[i])<>0) then
s := s+'仟';
end;
11: begin
if (StrToInt(Intstr[i])<>0) then
s := s+'佰';
end;
10: begin
if (StrToInt(Intstr[i])<>0) then
begin
//if (copy(s,Length(s)-1,2) ='壹')then
// s := copy(s,0,Length(s)-2);
s := s+'拾';
end;
end;
9: begin
if (StrToInt(Intstr[i])<>0) then
s := s+'亿'
else
begin
if (copy(s,Length(s)-1,2) ='零')then
s := copy(s,0,Length(s)-2);
s := s+'亿';
end;
end;
8: begin
if (StrToInt(Intstr[i])<>0) then
s := s+'仟';
end;
7: begin
if (StrToInt(Intstr[i])<>0)then
s := s+'佰';
end;
6: begin
if (StrToInt(Intstr[i])<>0) then
begin
if (copy(s,Length(s)-1,2) ='壹')then
s := copy(s,0,Length(s)-2);
s := s+'拾';
end;
end;
5: begin
if (StrToInt(Intstr[i])<>0) then
s := s+'万'
else
begin
s := copy(s,0,Length(s)-2);
if (copy(s,Length(s)-1,2) <>'亿')then
s := s+'万'
else
s := s+'零';
end;
end;
4: begin
if (StrToInt(Intstr[i])<>0) then
s := s+'仟';
end;
3: begin
if (StrToInt(Intstr[i])<>0) then
s := s+'佰';
end;
2: begin
if (StrToInt(Intstr[i])<>0) then
s := s+'拾';
end;
1: begin
if (copy(s,Length(s)-1,2) ='零')then
s := copy(s,0,Length(s)-2);
s := s+'元';
end;
end;
end;
For i := 1 to declen do
begin
Case StrToInt(decstr[i]) of
0: begin
if (copy(s,Length(s)-1,2)<>'零') then
s := s+'零';
end;
1: s := s+'壹';
2: s := s+'贰';
3: s := s+'叁';
4: s := s+'肆';
5: s := s+'伍';
6: s := s+'陆';
7: s := s+'柒';
8: s := s+'捌';
9: s := s+'玖';
end;
case i of
1: begin
if (StrToInt(decstr[i])<>0)then
s := s+'角';
end;
2: begin
if (StrToInt(decstr[i])<>0) then
s := s+'分';
end;
end;
end;
Result := s;
end;
最近写了两个小工具,要将小数部分四舍五入至一位小数和整数,可函数中round()是所谓的银行家进位法,实在用不了。无奈找度娘询问了一阵,结果不尽人情呀,最后找到一个保留两位小数的代码,通过修改后令人满意。现分享大家留着备用:
============================
保留1位小数
===========================
function BL1(s: real): real;
var
r1, r2: real;
s1, s2: string;
begin
r1 := int(s);
r2 := frac(s);
s1 := copy(floattostr(r1), 1, length(floattostr(r1)));
if length(floattostr(r2)) >= 4 then
begin
if strtoint(copy((floattostr(r2)), 4, 1)) >= 5 then //小数第三位开始复制一位0.00
if strtoint(copy((floattostr(r2)), 3, 1)) = 9 then
begin
s1 := inttostr(strtoint(s1) + 1);
s2 := '';
end
else
S2 := inttostr(strtoint(copy((floattostr(r2)), 3, 1)) + 1)
else if copy((floattostr(r2)), 3, 1) = '0' then
s2:=''
else s2 := copy(floattostr(r2), 3, 1);
end
else s2 := copy(floattostr(r2), 3, 1);
result := strtofloat(s1 + '.' + s2);
end;
====================
调用
=====================
label3.Caption :=floattostr(BL(strtofloat(edit1.text)));
================================
四舍五入到整数
================================
function BL0(s: real): real;
var
r1, r2: real;
s1, s2: string;
begin
r1 := int(s); //取整数部分
r2 := frac(s); //取小数部分
s1 := copy(floattostr(r1), 1, length(floattostr(r1))); //复制R1,复制位数为R1长度
if length(floattostr(r2)) >= 3 then //如果小数部分长度>=3
if strtoint(copy((floattostr(r2)), 3, 1)) >= 5 then //从小数部分第三位开始复制一位数
s1 := inttostr(strtoint(s1) + 1); //如果第一位小数大于5就向整数个位进1
result := strtofloat(s1);
end;
========================================================
四舍五入保留两位代码
========================================================
function BL2(s: real): real;
var
r1, r2: real;
s1, s2: string;
begin
r1 := int(s);
r2 := frac(s);
s1 := copy(floattostr(r1), 1, length(floattostr(r1)));
if length(floattostr(r2)) >= 5 then
begin
if strtoint(copy((floattostr(r2)), 5, 1)) >= 5 then
if strtoint(copy((floattostr(r2)), 4, 1)) = 9 then
if strtoint(copy((floattostr(r2)), 3, 1)) = 9 then
begin
s1 := inttostr(strtoint(s1) + 1);
s2 := '';
end
else
S2 := inttostr(strtoint(copy((floattostr(r2)), 3, 1)) + 1)
else if copy((floattostr(r2)), 3, 1) = '0' then
S2 := '0' + inttostr(strtoint(copy(floattostr(r2), 3, 2)) + 1)
else s2 := inttostr(strtoint(copy(floattostr(r2), 3, 2)) + 1)
else s2 := copy(floattostr(r2), 3, 2);
end
else s2 := copy(floattostr(r2), 3, 2);
result := strtofloat(s1 + '.' + s2);
end;
================================================
大小写转换
================================================
function TForm1.ConvertMoney(Num: Real): String;
var
intstr,decstr,s: String;
intlen,declen,i: word;
begin
Intstr:= intToStr(Trunc(Num));
decstr := FloatToStr(RoundTo(Frac(num),-1));//对小数进行四舍五入
//decstr := FloatToStr(RoundTo(Frac(num),-2));//对小数进行四舍五入
decstr := copy(decstr,3,Length(decstr)-1);
declen := Length(decstr);
intlen := Length(Intstr);
For i := 1 to Intlen do
begin
Case StrToInt(Intstr[i]) of
0: begin
if (copy(s,Length(s)-1,2)<>'零') then
s := s+'零';
end;
1: s := s+'壹';
2: s := s+'贰';
3: s := s+'叁';
4: s := s+'肆';
5: s := s+'伍';
6: s := s+'陆';
7: s := s+'柒';
8: s := s+'捌';
9: s := s+'玖';
end;
case intlen-i+1 of
13: begin
if (StrToInt(Intstr[i])<>0)then
s := s+'万';
end;
12: begin
if (StrToInt(Intstr[i])<>0) then
s := s+'仟';
end;
11: begin
if (StrToInt(Intstr[i])<>0) then
s := s+'佰';
end;
10: begin
if (StrToInt(Intstr[i])<>0) then
begin
//if (copy(s,Length(s)-1,2) ='壹')then
// s := copy(s,0,Length(s)-2);
s := s+'拾';
end;
end;
9: begin
if (StrToInt(Intstr[i])<>0) then
s := s+'亿'
else
begin
if (copy(s,Length(s)-1,2) ='零')then
s := copy(s,0,Length(s)-2);
s := s+'亿';
end;
end;
8: begin
if (StrToInt(Intstr[i])<>0) then
s := s+'仟';
end;
7: begin
if (StrToInt(Intstr[i])<>0)then
s := s+'佰';
end;
6: begin
if (StrToInt(Intstr[i])<>0) then
begin
if (copy(s,Length(s)-1,2) ='壹')then
s := copy(s,0,Length(s)-2);
s := s+'拾';
end;
end;
5: begin
if (StrToInt(Intstr[i])<>0) then
s := s+'万'
else
begin
s := copy(s,0,Length(s)-2);
if (copy(s,Length(s)-1,2) <>'亿')then
s := s+'万'
else
s := s+'零';
end;
end;
4: begin
if (StrToInt(Intstr[i])<>0) then
s := s+'仟';
end;
3: begin
if (StrToInt(Intstr[i])<>0) then
s := s+'佰';
end;
2: begin
if (StrToInt(Intstr[i])<>0) then
s := s+'拾';
end;
1: begin
if (copy(s,Length(s)-1,2) ='零')then
s := copy(s,0,Length(s)-2);
s := s+'元';
end;
end;
end;
For i := 1 to declen do
begin
Case StrToInt(decstr[i]) of
0: begin
if (copy(s,Length(s)-1,2)<>'零') then
s := s+'零';
end;
1: s := s+'壹';
2: s := s+'贰';
3: s := s+'叁';
4: s := s+'肆';
5: s := s+'伍';
6: s := s+'陆';
7: s := s+'柒';
8: s := s+'捌';
9: s := s+'玖';
end;
case i of
1: begin
if (StrToInt(decstr[i])<>0)then
s := s+'角';
end;
2: begin
if (StrToInt(decstr[i])<>0) then
s := s+'分';
end;
end;
end;
Result := s;
end;
相关文章推荐
- 小数位数的保留及大写转换代码
- JavaScript代码中浮点数据的保留小数位数的函数
- C#double转化成字符串 保留小数位数, 不以科学计数法的形式出现。
- C# decimal保留指定的小数位数,不四舍五入
- C#double转化成字符串 保留小数位数, 不以科学计数法的形式出现(转)
- (转)C++中保留小数位数的方法
- java.math.BigDecimal保留两位小数,保留小数,精确位数
- 【AngularJS】保留小数位数number
- 正则表达式判定保留2位小数的实现代码
- NumberFormat 可用于格式化和解析任何语言环境的数值。使代码能够完全独立于小数点、千位分隔符甚至所用特定小数位数的语言环境约定,并与数值格式是否为偶小数无关
- C#小数保留位数
- C#保留小数位数的方法集锦
- C# double和decimal数据类型以截断的方式保留指定的小数位数
- Java对小数保留指定位数方法
- javascript js保留小数位数的方法
- 2018_蓝桥_setprecision()函数_保留小数位数函数总结
- Java指定保留小数位数
- C#double转化成字符串 保留小数位数, 不以科学计数法的形式出现
- 【matlab】 关于保留小数位数&有效数字的问题
- 保留小数位数