我正在尝试乘以(rate * pps)* ts,并以金钱格式返回结果。我几乎让它工作了,但这不太对劲。
当我输入5.25作为比率的百分比,35.00表示pps,1200表示ts,结果是220500;与使用计算器计算时相同,但我需要以£2,205.00的格式返回
我到目前为止的代码是:
HTML:
<div class="row"><input type="text" name="rate" id="rate" placeholder="Dividend rate (%) e.g. 5.25" /></div>
<div class="row"><input type="text" name="pps" id="pps" placeholder="Par value (£) e.g. 1" /></div>
<div class="row"><input type="text" name="totalshares" id="totalshares" placeholder="Total number of shares e.g. 1000" /></div>
<div class="row"><input type="text" name="marketprice" id="marketprice" placeholder="Market price (£) e.g. 20" /></div>
<div class="row"><input type="button" value="Calculate dividend" onClick="calculateDividend()" /></div>
<div class="row" id="dividendcont" style="display:none"><input type="text" name="dividend" id="dividend" placeholder="Dividend" /></div>
javascript:
function calculateDividend(){
var rate = document.getElementById('rate').value;
var pps = document.getElementById('pps').value;
var ts = document.getElementById('totalshares').value;
var dividend = (rate * pps) * ts;
document.getElementById('dividendcont').style.display="block";
document.getElementById('dividend').value = dividend;
}
非常感谢任何帮助。
更新: 谢谢马特。这让我走上正轨。 这是我最终如何做到的:
JS:
function calculateDividend(){
var rate = document.getElementById('rate').value;
var pps = document.getElementById('pps').value;
var ts = document.getElementById('totalshares').value;
var mp = document.getElementById('marketprice').value;
var dividend = (rate * pps) * ts;
var total = number_format(dividend / 100, 2, ".", ",");
document.getElementById('dividendcont').style.display="block";
document.getElementById('dividend').value = "£"+total;
}
function number_format (number, decimals, dec_point, thousands_sep) {
// Strip all characters but numerical ones.
number = (number + '').replace(/[^0-9+\-Ee.]/g, '');
var n = !isFinite(+number) ? 0 : +number,
prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
s = '',
toFixedFix = function (n, prec) {
var k = Math.pow(10, prec);
return '' + Math.round(n * k) / k;
};
// Fix for IE parseFloat(0.55).toFixed(0) = 0;
s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
if (s[0].length > 3) {
s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
}
if ((s[1] || '').length < prec) {
s[1] = s[1] || '';
s[1] += new Array(prec - s[1].length + 1).join('0');
}
return s.join(dec);
}
感谢大家的帮助
答案 0 :(得分:0)
你只需要除以100即可将你得到的美分变为美元,然后加上逗号和英镑符号。
以下是一个深入的答案,其中介绍了如何将数字格式化为金钱:How can I format numbers as money in JavaScript?
答案 1 :(得分:0)
“最现代”的答案是:
(value/100).toLocaleString("en-gb",{style:'currency',currency:'GBP',minimumFractionDigits:2})
但是,这在各种旧浏览器中都会失败(请参阅the documentation)。
答案 2 :(得分:0)
有兴趣了解在不支持的浏览器(或number.toLocaleString
不支持的单元)中实现货币格式的功能会是什么样的,想出这个,默认货币是GBP
function currency(val, unit, tsep, usep, rounder) {
var re = /(\d+)(?=(?:\d{3})+)/g,
arr = [];
if (typeof val !== 'number' || val !== val || val === Infinity || val === -Infinity)
throw TypeError('Invalid value: ' + val);
if (typeof unit !== 'string') unit = '£';
if (typeof tsep !== 'string') tsep = ',';
if (typeof usep !== 'string') usep = '.';
if (typeof rounder !== 'function') rounder = Math.round;
if (val < 0)
unit = unit + ' -', val = -val;
arr[0] = Math.floor(val);
arr[1] = '' + rounder((val * 100) - (arr[0] * 100));
arr[0] = ('' + arr[0]).replace(re, '$1' + tsep);
if (arr[1] === '0')
arr[1] = ''; // or `usep + '00'`
else {
arr[1] = usep + '00'.slice(arr[1].length) + arr[1];
}
return unit + arr[0] + arr[1];
}
用法示例,
currency(220500 / 100); // "£2,205"
currency(-220500 / 100); // "£ -2,205"