星空多行文本控件数字类型字符对齐方法
星空多行文本控件数字类型字符对齐方法
开发项目上有时需要在多行文本控件中写入不同栏位的信息,但写入数字后对齐方式设置经常出现错位,没法对齐如下图:
考虑到大部分项目中统计金额不会超过13位数(不包含小数点部分),业务上最大的13位数9999999999999是玖万玖千玖佰玖拾玖亿 玖千玖佰玖拾玖万 玖千玖佰玖拾玖,因此按13位数,保留两位小数点来显示汇总结果,不足13位的补空格显示,保证统计金额靠右对齐。
开发过程中发现,不同长度保留2位小数点按千分位格式展示的字符串需要补空格的位数如下图:
样本数据(字符串格式) | 数字长度 | 保留2位小数点 千分位展示字符长度 | 补空格位数 |
3,219,876,543,210.12 | 13 | 20 | 0 |
219,876,543,210.12 | 12 | 18 | 3 |
19,876,543,210.12 | 11 | 17 | 5 |
9,876,543,210.12 | 10 | 16 | 7 |
876,543,210.12 | 9 | 14 | 10 |
76,543,210.12 | 8 | 13 | 12 |
6,543,210.12 | 7 | 12 | 14 |
543,210.12 | 6 | 10 | 17 |
43,210.12 | 5 | 9 | 19 |
3,210.12 | 4 | 8 | 21 |
210.12 | 3 | 6 | 24 |
10.12 | 2 | 5 | 26 |
0.12 | 1 | 4 | 28 |
.1.1.1 主要代码:
/**
* 获取字符串的字节长度,不考虑中文字节
*
* @param chstring
* @return
*/
public static int CalLength(String chstring)
{
int length = 0;
if (chstring.IsNullOrEmptyOrWhiteSpace())
return 0;
for (int i = 0; i < chstring.Length; i++)
{
char c = chstring.CharAt(i);
length += 1;
}
return length;
}
public static String FullWithBank(String chstring,int bankLen)
{
int chlen = CalLength(chstring);
String spance = " ";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bankLen; i++)
{
sb.Append(spance);
}
return sb.ToString() + chstring;
}
/**
* 保留2位小数点小于或等于13位数字格式的字符串,不足13位的用空格符显示,保证数字靠右对齐。
* 主要是因为千分位格式的数字,补空格符的引起靠右对齐问题
* @param chstring(保留2位小数点小于或等于13位数字,字符串格式)
* @return chstring
*/
public static String FullWithBank(String chstring)
{
int chlen = CalLength(chstring);
int bankLen = 0;
String spance = " ";
StringBuilder sb = new StringBuilder();
//20 0:3,219,876,543,210.12
if (chlen == 20)
{
bankLen = 0;
}
//18 3:219,876,543,210.12
else if (chlen == 18)
{
bankLen = 3;
}
//17 5:19,876,543,210.12
else if (chlen == 17)
{
bankLen = 5;
}
//16 7:9,876,543,210.12
else if (chlen == 16)
{
bankLen = 7;
}
//14 10:876,543,210.12
else if (chlen == 14)
{
bankLen = 10;
}
//13 12:76,543,210.12
else if (chlen == 13)
{
bankLen = 12;
}
//12 14:6,543,210.12
else if (chlen == 12)
{
bankLen = 14;
}
//10 17:543,210.12
else if (chlen == 10)
{
bankLen = 17;
}
//9 19:43,210.12
else if (chlen == 9)
{
bankLen = 19;
}
//8 21:3,210.12
else if (chlen == 8)
{
bankLen = 21;
}
//6 24:210.12
else if (chlen == 6)
{
bankLen = 24;
}
//5 26:10.12
else if (chlen == 5)
{
bankLen = 26;
}
//4 28:0.12
else if (chlen == 4)
{
bankLen = 28;
}
for (int i = 0; i < bankLen; i++)
{
sb.Append(spance);
}
return sb.ToString() + chstring;
}
.1.1.2 调用样本数据
lineDesc = "70005030-1103:" + FullWithBank("9,999,999,999,999.99");
70005030 - 1103: 3,219,876,543,210.12
promotionDesc += "\r\n" + lineDesc;
lineDesc = "70005030-1103:" + FullWithBank("222,111,000,111.66");
70001030 - 1103: 219,876,543,210.12
promotionDesc += "\r\n" + lineDesc;
lineDesc = "70005030-1103:" + FullWithBank("33,111,111,111.11");
70001030 - 1103: 19,876,543,210.12
promotionDesc += "\r\n" + lineDesc;
lineDesc = "70005030-1103:" + FullWithBank("9,876,543,210.12");
70001030 - 1103: 9,876,543,210.12
promotionDesc += "\r\n" + lineDesc;
lineDesc = "70005030-1103:" + FullWithBank("876,543,210.12");
70001030 - 1103: 876,543,210.12
promotionDesc += "\r\n" + lineDesc;
lineDesc = "70005030-1103:" + FullWithBank("76,543,210.12");
70001030 - 1103: 76,543,210.12
promotionDesc += "\r\n" + lineDesc;
lineDesc = "70005030-1103:" + FullWithBank("6,543,210.12");
70001030 - 1103: 6,543,210.12
promotionDesc += "\r\n" + lineDesc;
lineDesc = "70005030-1103:" + FullWithBank("543,210.12");
70001030 - 1103: 543,210.12
promotionDesc += "\r\n" + lineDesc;
lineDesc = "70005030-1103:" + FullWithBank("43,210.12");
70001030 - 1103: 43,210.12
promotionDesc += "\r\n" + lineDesc;
lineDesc = "70005030-1103:" + FullWithBank("5,111.12");
70001030 - 1103: 3,210.12
promotionDesc += "\r\n" + lineDesc;
lineDesc = "70005030-1103:" + FullWithBank("783.12");
70001030 - 1103: 210.12
promotionDesc += "\r\n" + lineDesc;
lineDesc = "70005030-1103:" + FullWithBank("11.11");
70001030 - 1103: 10.12
promotionDesc += "\r\n" + lineDesc;
lineDesc = "70005030-1103:" + FullWithBank("0.00");
70001030 - 1103: 0.12
promotionDesc += "\r\n" + lineDesc;
显示结果
<全文完>
星空多行文本控件数字类型字符对齐方法
本文2024-09-16 17:12:56发表“云星空知识”栏目。
本文链接:https://wenku.my7c.com/article/kingdee-k3cloud-14273.html