Skip to content

Commit

Permalink
Add currency internationalization support, and remove dollar signs fr…
Browse files Browse the repository at this point in the history
…om output templates.
  • Loading branch information
jpederson committed Apr 6, 2019
1 parent f2e2ca6 commit e0b9bbd
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
sftp-config.json
node_modules
.sass-cache
example.css.map
package-lock.json
4 changes: 1 addition & 3 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = function(grunt) {
watch: {
js: {
files: ['jquery.accrue.js'],
tasks: ['uglify', 'jshint'],
tasks: ['jshint','uglify'],
options: {
livereload: true,
},
Expand Down Expand Up @@ -46,13 +46,11 @@ module.exports = function(grunt) {
}
},


// lint me.
jshint: {
all: ['jquery.accrue.js']
}


});

// register task
Expand Down
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ <h3>The HTML</h3>

<h3>More Documentation</h3>
<p>View the <a href="https://github.com/jpederson/Accrue.js/wiki">wiki</a> for detailed instructions about any of the calculator modes and all the additional options and features.</p>
<p class="buttons"><a href="https://github.com/jpederson/Accrue.js/wiki" target="_blank" class="btn">View the Wiki</a></p>

</div>
</div>
Expand Down
43 changes: 24 additions & 19 deletions jquery.accrue.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,9 @@
$.fn.accrue.options = {
mode: "basic",
operation: "keyup",
currency: "USD",
default_values: {
amount: "$7,500",
amount: "7,500",
rate: "7%",
rate_compare: "1.49%",
term: "36m"
Expand All @@ -152,20 +153,24 @@
},
response_output_div: ".results",
response_basic:
'<p><strong>Monthly Payment:</strong><br />$%payment_amount%</p>'+
'<p><strong>Monthly Payment:</strong><br />%payment_amount%</p>'+
'<p><strong>Number of Payments:</strong><br />%num_payments%</p>'+
'<p><strong>Total Payments:</strong><br />$%total_payments%</p>'+
'<p><strong>Total Interest:</strong><br />$%total_interest%</p>',
response_compare: '<p class="total-savings">Save $%savings% in interest!</p>',
'<p><strong>Total Payments:</strong><br />%total_payments%</p>'+
'<p><strong>Total Interest:</strong><br />%total_interest%</p>',
response_compare: '<p class="total-savings">Save %savings% in interest!</p>',
error_text: '<p class="error">Please fill in all fields.</p>',
callback: function ( elem, data ){}
};

// FORMAT MONEY
// This function is used to add thousand seperators to numerical ouput
// as a means of properly formatting money
function formatNumber (num) {
return num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,");
function formatNumber( num, options ) {
var formatted = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: options.currency
}).format( num );
return formatted.replace('$$','$');
}

// GET FIELD
Expand Down Expand Up @@ -228,10 +233,10 @@

// replace the placeholders with the response values.
var output_content = options.response_basic
.replace( "%payment_amount%", formatNumber(loan_info.payment_amount_formatted) )
.replace( "%payment_amount%", formatNumber( loan_info.payment_amount_formatted, options ) )
.replace( "%num_payments%", loan_info.num_payments )
.replace( "%total_payments%",formatNumber(loan_info.total_payments_formatted) )
.replace( "%total_interest%", formatNumber(loan_info.total_interest_formatted) );
.replace( "%total_payments%",formatNumber( loan_info.total_payments_formatted, options ) )
.replace( "%total_interest%", formatNumber( loan_info.total_interest_formatted, options ) );

// output the content to the actual output element.
output_elem.html( output_content );
Expand Down Expand Up @@ -292,15 +297,15 @@
// replace our savings placeholder in the response text with
// the real difference in interest.
var output_content = options.response_compare
.replace( "%savings%", formatNumber(callback_data.savings.toFixed(2)) )
.replace( "%loan_1_payment_amount%", formatNumber(loan_2_info.payment_amount_formatted) )
.replace( "%savings%", formatNumber( callback_data.savings.toFixed(2), options ) )
.replace( "%loan_1_payment_amount%", formatNumber( loan_2_info.payment_amount_formatted, options ) )
.replace( "%loan_1_num_payments%", loan_2_info.num_payments )
.replace( "%loan_1_total_payments%", loan_2_info.total_payments_formatted )
.replace( "%loan_1_total_interest%", formatNumber(loan_2_info.total_interest_formatted) )
.replace( "%loan_2_payment_amount%", formatNumber(loan_1_info.payment_amount_formatted) )
.replace( "%loan_1_total_interest%", formatNumber( loan_2_info.total_interest_formatted, options ) )
.replace( "%loan_2_payment_amount%", formatNumber( loan_1_info.payment_amount_formatted, options ) )
.replace( "%loan_2_num_payments%", loan_1_info.num_payments )
.replace( "%loan_2_total_payments%", loan_1_info.total_payments_formatted )
.replace( "%loan_2_total_interest%", formatNumber(loan_1_info.total_interest_formatted) );
.replace( "%loan_2_total_interest%", formatNumber( loan_1_info.total_interest_formatted, options ) );
output_elem.html( output_content );

} else {
Expand Down Expand Up @@ -366,10 +371,10 @@
output_content = output_content+
'<tr>'+
'<'+cell_tag+' class="accrue-payment-number">'+(i+1)+'</'+cell_tag+'>'+
'<'+cell_tag+' class="accrue-payment-amount">$'+formatNumber(loan_info.payment_amount_formatted)+'</'+cell_tag+'>'+
'<'+cell_tag+' class="accrue-total-interest">$'+formatNumber(counter_interest.toFixed(2))+'</'+cell_tag+'>'+
'<'+cell_tag+' class="accrue-total-payments">$'+formatNumber(counter_payment.toFixed(2))+'</'+cell_tag+'>'+
'<'+cell_tag+' class="accrue-balance">$'+formatNumber(counter_balance.toFixed(2))+'</'+cell_tag+'>'+
'<'+cell_tag+' class="accrue-payment-amount">'+formatNumber( loan_info.payment_amount_formatted, options )+'</'+cell_tag+'>'+
'<'+cell_tag+' class="accrue-total-interest">'+formatNumber( counter_interest.toFixed(2), options )+'</'+cell_tag+'>'+
'<'+cell_tag+' class="accrue-total-payments">'+formatNumber( counter_payment.toFixed(2), options )+'</'+cell_tag+'>'+
'<'+cell_tag+' class="accrue-balance">'+formatNumber( counter_balance.toFixed(2), options )+'</'+cell_tag+'>'+
'</tr>';
}

Expand Down
2 changes: 1 addition & 1 deletion jquery.accrue.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{
"name" : "accrue",
"version" : "0.0.2",
"name": "accrue",
"version": "0.1.0",
"repository": {
"type": "git",
"url": "git://github.com/jpederson/Accrue.js.git"
},
"devDependencies" : {
"grunt" : "latest",
"grunt-contrib-cssmin": "*",
"grunt-contrib-sass": "*",
"grunt-contrib-uglify": "*",
"grunt-contrib-jshint": "*",
"grunt-contrib-watch": "*",
"matchdep": "*"
"devDependencies": {
"grunt": "latest",
"grunt-contrib-cssmin": "*",
"grunt-contrib-sass": "*",
"grunt-contrib-uglify": "*",
"grunt-contrib-jshint": "*",
"grunt-contrib-watch": "*",
"matchdep": "*"
}
}
}

0 comments on commit e0b9bbd

Please sign in to comment.