Improved decimal tests.
This commit is contained in:
parent
3aea8321b4
commit
54ea23a330
2 changed files with 16 additions and 4 deletions
|
@ -551,6 +551,8 @@ class App.Utils
|
||||||
# format decimal
|
# format decimal
|
||||||
@decimal: (data, positions = 2) ->
|
@decimal: (data, positions = 2) ->
|
||||||
|
|
||||||
|
data = data.toString()
|
||||||
|
|
||||||
# input validation
|
# input validation
|
||||||
return data if data is ''
|
return data if data is ''
|
||||||
return data if data.match(/[A-z]/)
|
return data if data.match(/[A-z]/)
|
||||||
|
@ -560,16 +562,16 @@ class App.Utils
|
||||||
num = num + '0'
|
num = num + '0'
|
||||||
num
|
num
|
||||||
|
|
||||||
result = data.toString().match(/^(.+?)\.(.+?)$/)
|
result = data.match(/^(.+?)\.(.+?)$/)
|
||||||
|
|
||||||
# add .00
|
# add .00
|
||||||
if !result || !result[2]
|
if !result || !result[2]
|
||||||
return "#{data}." + format( 0, positions ).toString()
|
return "#{data}." + format(0, positions)
|
||||||
length = result[2].toString().length
|
length = result[2].length
|
||||||
diff = positions - length
|
diff = positions - length
|
||||||
|
|
||||||
# check length, add .00
|
# 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
|
# check length, remove longer positions
|
||||||
"#{result[1]}.#{result[2].substr(0,positions)}"
|
"#{result[1]}.#{result[2].substr(0,positions)}"
|
||||||
|
|
|
@ -1221,6 +1221,16 @@ test( "check decimal format", function() {
|
||||||
verify = App.Utils.decimal( string )
|
verify = App.Utils.decimal( string )
|
||||||
equal( verify, result, 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'
|
string = '111111.6'
|
||||||
result = '111111.60'
|
result = '111111.60'
|
||||||
verify = App.Utils.decimal( string )
|
verify = App.Utils.decimal( string )
|
||||||
|
|
Loading…
Reference in a new issue