Improved decimal tests.

This commit is contained in:
Martin Edenhofer 2015-10-03 09:25:40 +02:00
parent 3aea8321b4
commit 54ea23a330
2 changed files with 16 additions and 4 deletions

View file

@ -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)}"

View file

@ -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 )