java银行金额格式化_Java 格式化問題:我要写一个程序来格式化一串代表银行帐号的数字。...

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

import java.util.Properties;

public class FormateBankAccountId {

List standardBankAccountIdFormat ;

public FormateBankAccountId(String propertiesFileName) throws FileNotFoundException, IOException

{

// 加载资源文件

Properties properties = new Properties();

properties.load(new FileReader(propertiesFileName));

this.standardBankAccountIdFormat = new ArrayList();

int keyNumber = 1;

String value = null;

// 读取键值对,键的格式是: formate_1 ,..., formate_10,...,formate_100 等

while( ( value = properties.getProperty("formate_" + keyNumber++) ) != null)

{

value = value.trim();

this.standardBankAccountIdFormat.add(value);

}

}

public List formate(String orginalBankAccountId)

{

List proceededlBankAccountIds = new ArrayList();

orginalBankAccountId = orginalBankAccountId.trim();

for(String formateString : this.standardBankAccountIdFormat)

{

// 格式字符串中的数字个数,

int formateStringDigitNum = formateString.replace("-", "").length();

//原字符串过长,截取后面部分

if(orginalBankAccountId.length() > formateStringDigitNum)

{

orginalBankAccountId = orginalBankAccountId.substring(orginalBankAccountId.length() - formateStringDigitNum);

}

//原字符串过短,前面补零

else if(orginalBankAccountId.length() < formateStringDigitNum)

{

String pre = "";

for(int i = 0 ; i < formateStringDigitNum - orginalBankAccountId.length() ; i ++) pre += "0";

orginalBankAccountId = pre + orginalBankAccountId;

}

// 格式化字符串中分隔符位置

int delimiterPosition = 0;

String temp = orginalBankAccountId;

while(-1 != (delimiterPosition = formateString.indexOf('-',delimiterPosition)))

{

// 添加分割符号

temp = temp.substring(0,delimiterPosition) + '-' + temp.substring(delimiterPosition );

delimiterPosition ++;

}

proceededlBankAccountIds.add(temp);

}

return proceededlBankAccountIds;

}

/**

* @param args

* @throws IOException

* @throws FileNotFoundException

*/

public static void main(String[] args) throws FileNotFoundException, IOException {

// TODO Auto-generated method stub

String orginalBankAccountId = "000111111222";

FormateBankAccountId formateTool = new FormateBankAccountId("bankAccountId.properties");

for(String proceededBankAccountId: formateTool.formate(orginalBankAccountId))

{

System.out.println(proceededBankAccountId);

}

}

}

/*

bankAccountId.properties:

formate_1 ###-######-###

formate_2 ##-####-###-##

formate_3 ####-#-###-###

formate_4 #-########-###

formate_5 ###-########-#

*/

/*

output :

000-111111-222

00-1111-112-22

0011-1-111-222

0-00111111-222

000-11111122-2

*/

取消

评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值