linux 如何将输入余额22222222.22转换为德语格式2.222.222,22

v09wglhw  于 2023-08-03  发布在  Linux
关注(0)|答案(2)|浏览(131)

我尝试转换的值从输入XML像2222222.22 OT德国格式像2.222.222,22.下面是带有while函数的代码块

# Remove any existing dots as thousand separators and replace dot with comma as decimal separator
            tag2val[tags[i]] = gensub(/\./, ",", "g", tag2val[tags[i]])
            # Add dots as thousand separators if necessary
            while (match(tag2val[tags[i]], /[0-9]{4}/)) {
                tag2val[tags[i]] = substr(tag2val[tags[i]], 1, RSTART) "." substr(tag2val[tags[i]], RSTART+1)
            }

字符串
当然,这个脚本需要工作,无论值是2.22或222.22或2222222.22任何帮助?

b1payxdu

b1payxdu1#

只需将您的区域设置为显示所需格式的数字的区域,例如:

$ echo '2222222.22' | LC_ALL=de_DE awk '{printf "%\047.02f\n", $0}'
2.222.222,22

$ echo '2222222.22' | LC_ALL=da_DK awk '{printf "%\047.02f\n", $0}'
2.222.222,22

字符串
请参阅https://stackoverflow.com/a/61911355/1745001了解其他可能的语言环境,以及如何在上述两种语言环境都不适合的情况下找到适合您的语言环境。

egmofgnx

egmofgnx2#

一个“C风格”的解决方案,适用于任何Awk:

$ cat data.txt
2.22
22.22
222.22
2222.22
22222.22
222222.22
2222222.22

字符串

$ cat filter.awk
{
   len = split( $0, a, "" ) 
   for ( i = len; i > 0; i-- )  # traverse the record character by character, 
   {                            #> in reverse: -> 22.2222222 ->
      n++
      m++

      if ( n == 3 )  # n: comma position counter: 
      {              #> 3rd pos? replace the dot with comma
         a[i] = ","
         m -= 4      # m: dot position counter: 
      }              #> decreased to -1 at the 3rd pos, will be 3 at the 7th pos

      if (( n > 6 ) && ( m % 3 == 0 ))  # we are at the 7th pos, time to insert a dot
      {
         rec = "." rec  # to get the reversed record right, build it in reverse
      }

      rec = a[i] rec  # ditto
   }

   print rec
   len = i = n = m = rec = ""
}

$ awk -f filter.awk data.txt
2,22
22,22
222,22
2.222,22
22.222,22
222.222,22
2.222.222,22

相关问题