如何将Json Number转换为给定的用户格式? [英] How Can I Convert Json Number into given user format ?

查看:150
本文介绍了如何将Json Number转换为给定的用户格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将Json Number字符串转换为不同的格式?



我得到Json的数字



How can I convert a Json Number string to different format?

I get the Json numebr like that

Amount": 816673.5789





这里Amount是列名。



我需要使用Javascript编写一个函数。这个函数有两个参数,一个是'Amount',另一个是'NumberFormate'。我需要将'Amount'转换为给定日期' NumberFormate'和函数将返回此数字。



这里'NumberFormate'表示我们需要在点数后计算的数量。

示例:



如果用户给出的格式是这样的话





Here Amountis the column name.

I need to write a function using Javascript. This functing has two parameter one is this 'Amount', another is 'NumberFormate'. I need to convert this 'Amount' as given date 'NumberFormate' and function will return this number.

Here 'NumberFormate' indicate how many number we need to count after point.
Example:

If user given format is like that

##.## 





函数返回号为8,16,673.57



如果用户给出的格式是这样的话





the the function return number as 8,16,673.57

If user given format is like that

##.### 





函数返回数为8,16,673.578



请注意,返回时该数字将以逗号分隔。



the the function return number as 8,16,673.578

Please remind that the number wil be comma separated when it return.

推荐答案

您可以在格式化中使用JavaScript构建正则表达式...

You can use JavaScript build in formatting and regex to do so...
n.toFixed(2).replace(/\d(?=(\d{3})+\.)/, '


&,');
&,');



其中n是数字,toFixed (2)截断小数点后的数字,正则表达式在每组3之前添加逗号(,)...

http://www.regexper.com/#%5Cd(%3F%3D(%5CD%7B3%7D)% 2B%5C。) [ ^ ]


这篇关于如何将Json Number转换为给定的用户格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆