chenshijin1 / articles-development Goto Github PK
View Code? Open in Web Editor NEWArticles published in terms of development
Articles published in terms of development
JS对货币格式化函数
1001. A+B Format (20)-PAT甲级真题
/**
* 将数值四舍五入(保留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"));
/*
除了第一位是负号的情况,
只要当前位的下标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"));
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.