1. //分换算元
    2. export function toMoney(num: any) {
    3. num = parseFloat(num) / 100;
    4. num += '';
    5. num = num.replace(/[^0-9|\.]/g, '');
    6. if (/^0+/) {
    7. num = num.replace(/^0+/, '');
    8. }
    9. if (!/\./.test(num)) {
    10. num += '.00';
    11. }
    12. if (/^\./.test(num)) {
    13. num = '0' + num;
    14. }
    15. num += '00';
    16. num = num.match(/\d+\.\d{2}/)[0];
    17. return num;
    18. }
    1. // 格式化 金额 获取整数部分
    2. const getInteger = (val: number) => {
    3. const num = val / 100;
    4. const result = Number(num.toString().match(/^\d+(?:\.\d{0,2})?/));
    5. let s = result.toString();
    6. let rs = s.indexOf('.');
    7. if (rs < 0) {
    8. rs = s.length;
    9. s += '.';
    10. }
    11. while (s.length <= rs + 2) {
    12. s += '0';
    13. }
    14. let htmlStr = s.split('.')[0];
    15. return htmlStr;
    16. }
    17. // 格式化 金额 获取小数部分
    18. const getDecimal = (val: number) => {
    19. const num = val / 100;
    20. const result = Number(num.toString().match(/^\d+(?:\.\d{0,2})?/));
    21. let s = result.toString();
    22. let rs = s.indexOf('.');
    23. if (rs < 0) {
    24. rs = s.length;
    25. s += '.';
    26. }
    27. while (s.length <= rs + 2) {
    28. s += '0';
    29. }
    30. let htmlStr = s.split('.')[1];
    31. return htmlStr;
    32. }