Giter VIP home page Giter VIP logo

articles-development's People

Contributors

chenshijin1 avatar

Stargazers

 avatar

articles-development's Issues

JS对货币格式化

引子

JS对货币格式化函数
1001. A+B Format (20)-PAT甲级真题

JS对货币格式化函数

/**
 * 将数值四舍五入(保留1位小数)后格式化成金额形式
 *
 * @param num 数值(Number或者String)
 * @return 金额格式的字符串,如'1,234,567.4'
 * @type String
 */
function formatCurrencyTenThou(num) {
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
    num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*10+0.50000000001);
    cents = num%10;
    num = Math.floor(num/10).toString();
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+','+
    num.substring(num.length-(4*i+3));
    return (((sign)?'':'-') + num + '.' + cents);
}
console.log(formatMoney("-1234567.4512"));

A+B Format (20)-PAT甲级真题

/*
除了第一位是负号的情况,
只要当前位的下标i满足(i + 1) % 3 == len % 3并且i不是最后一位,
就在逐位输出的时候在该位输出后的后面加上一个逗号
 */
#include <iostream>
using namespace std;
int main() {
    int a, b;
    cin >> a >> b;
    string s = to_string(a + b);
    int len = s.length();
    for (int i = 0; i < len; i++) {
        cout << s[i];
        if (s[i] == '-') continue;
        if ((i + 1) % 3 == len % 3 && i != len - 1) cout << ",";
    }
    return 0;
}

这几天同时看到了这份代码,有相似性。
觉得算法版的三位一隔更加简洁,清晰,所以就结合一下。

/*
把金额转化为千进制格式
123456 -> 123,456
*/
function formatMoney(num) {
    num = num.toString().replace(/\$|\,/g, '');
    if (isNaN(num))
        num = "0";

    /*
    if(num==正数)
    sign=true;
    else(num==负数)
    sign=false;
    * */
    sign = (num == Math.abs(num));
    num = Math.abs(num);

    /*
    百分位四舍五入
    cents: 小数部分
    num: 整数部分
    * */
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10)
        cents = "0" + cents;

    /*
    三位一隔
    */
    var number="";
    var len=num.length;
    for (var i = 0; i < len ; i++) {
        console.log(num[i]);
        number+=num[i];
        if ((i + 1) % 3 == len % 3 && i != len - 1) {
            // console -1,234,567.46
            //第7位,第4位时打印","
            number+=",";
        }
    }

    return (((sign) ? '' : '-') + number + '.' + cents);
}
console.log(formatMoney("-1234567.4512"));

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.