From 54ea23a33006f2581ac493f4c602dd15d694d02a Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Sat, 3 Oct 2015 09:25:40 +0200 Subject: [PATCH] Improved decimal tests. --- app/assets/javascripts/app/lib/app_post/utils.coffee | 10 ++++++---- public/assets/tests/html-utils.js | 10 ++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/app/assets/javascripts/app/lib/app_post/utils.coffee b/app/assets/javascripts/app/lib/app_post/utils.coffee index 0c4ad90e0..de84df457 100644 --- a/app/assets/javascripts/app/lib/app_post/utils.coffee +++ b/app/assets/javascripts/app/lib/app_post/utils.coffee @@ -551,6 +551,8 @@ class App.Utils # format decimal @decimal: (data, positions = 2) -> + data = data.toString() + # input validation return data if data is '' return data if data.match(/[A-z]/) @@ -560,16 +562,16 @@ class App.Utils num = num + '0' num - result = data.toString().match(/^(.+?)\.(.+?)$/) + result = data.match(/^(.+?)\.(.+?)$/) # add .00 if !result || !result[2] - return "#{data}." + format( 0, positions ).toString() - length = result[2].toString().length + return "#{data}." + format(0, positions) + length = result[2].length diff = positions - length # check length, add .00 - return "#{result[1]}." + format( result[2], positions ).toString() if diff > 0 + return "#{result[1]}." + format(result[2], positions) if diff > 0 # check length, remove longer positions "#{result[1]}.#{result[2].substr(0,positions)}" diff --git a/public/assets/tests/html-utils.js b/public/assets/tests/html-utils.js index 52d1932e6..dee4507f2 100644 --- a/public/assets/tests/html-utils.js +++ b/public/assets/tests/html-utils.js @@ -1221,6 +1221,16 @@ test( "check decimal format", function() { verify = App.Utils.decimal( string ) equal( verify, result, string ) + string = '6' + result = '6.00' + verify = App.Utils.decimal( string ) + equal( verify, result, string ) + + string = 6.5 + result = '6.50' + verify = App.Utils.decimal( string ) + equal( verify, result, string ) + string = '111111.6' result = '111111.60' verify = App.Utils.decimal( string )