How can I format a decimal in xquery?

二次信任 提交于 2019-12-05 06:11:35

Use:

let $n := 5573652.23
 return
      concat(local:format-int(xs:int(floor($n))),
             '.',
             substring(string($n - floor($n)), 3)
             )

This produces exactly the wanted, correct result:

5,573,652.23

This doesn't work for you?:

format-number(5573652.23,",###.##")

You can play with this here. I am pretty sure that saxon supports this function.

Edit: This function is not supported in saxon (see comments below).

With XQuery 3.0 and Saxon-HE 9.7 Parser you can do the following:

declare decimal-format local:de decimal-separator = "," grouping-separator = ".";
declare decimal-format local:en decimal-separator = "." grouping-separator = ",";
let $numbers := (1234.567, 789, 1234567.765)
for $i in $numbers
return (
   format-number($i,"#.###,##","local:de"),
   format-number($i,"#,###.##","local:en")
)

The output is:

<?xml version="1.0" encoding="UTF-8"?>1.234,57 1,234.57 789,0 789.0 1.234.567,76 
1,234,567.76
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!