【问题标题】:New Zealand Bank Account Format Display新西兰银行账户格式显示
【发布时间】:2013-05-31 12:36:42
【问题描述】:

我将根据用户提供的自定义格式在报告中显示新西兰和澳大利亚的银行账户。例如,新西兰银行账户可以是 00-0000-0000000-000,最后一位数字(银行账户后缀)是可选的。格式有两部分:

  • 破折号的放置
  • 2 位前缀

示例格式的银行账户可以是 01-1234-1234567-55 和 01-1234-1234567-002。银行帐户存储在数据库中,没有任何格式。当我尝试 String.Format("{0:00-0000-0000000-00#}",121234123456712) 时,它没有返回预期的 12-1234-1234567-12,而是返回 01-2123-4123456-712。

可以理解,我总是可以测试银行账户的长度并做一个 switch 语句,但格式是用户定义的。

以下确保破折号放置正确且后缀正确:

// ensure there is a format to use and a bank account is present
if (bankaccountformat != "" && bankaccountformat.Contains('-') && bankaccount != "")
{
  int i = 0;
  foreach (char dash in bankaccountformat)
  {
    // add dash in bank account, if bank account is long enough
    if (dash == '-' && bankaccount.Length > i)
    {
      bankaccount = bankaccount.Insert(i, "-");
    }
    i++;
  }
}

问题不在于帐户是否有效,而在于格式。请让我知道格式化帐户的更好方法。

【问题讨论】:

    标签: account string.format bank


    【解决方案1】:

    您没有提及您使用哪种编程语言。无论如何,您似乎使用了一个数组。这可能很棘手。

    在 BASH 中,我会使用字符串操作,就像在这个脚本 (fmat) 中一样:

    #!/bin/bash
    
    #only numbers allowed
    [[ ! "$1" =~ ^[0-9]+$ ]] && echo "Error: enter numbers only" && exit
    
    s=$1
    l=${#s}
    r=l-13
    
    #minimal number of digits is 13, maximum 16
    [[ r -lt 0 || r -gt 3 ]] && echo "Error: enter 13-16 digits" && exit
    
    #first 13 digits
    a=${s:0:2}"-"${s:2:4}"-"${s:6:7}
    
    #when 14-16 digits
    a=$a"-"${s:13:r}
    
    echo $a
    

    fmat 011234123456755 给出 01-1234-1234567-55

    fmat 0112341234567002 给出 01-1234-1234567-002

    fmat 0112341234567 给出 01-1234-1234567

    如果您使用 BASH 以外的其他东西,那么至少这会给出一些提示。

    【讨论】:

    • 我忘了说我正在使用 C#
    • 哦,C# 中的字符串操作也很简单。我不使用 C#,因此无法提供脚本,但希望您能理解。
    猜你喜欢
    • 1970-01-01
    • 2017-10-10
    • 2015-09-01
    • 1970-01-01
    • 2014-08-27
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    • 2016-11-21
    相关资源
    最近更新 更多