Skip to content

Instantly share code, notes, and snippets.

@penso
Created January 24, 2018 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save penso/e11513b346b99063068ce01bf4ab0af4 to your computer and use it in GitHub Desktop.
Save penso/e11513b346b99063068ce01bf4ab0af4 to your computer and use it in GitHub Desktop.
test tva
#!/usr/bin/env ruby
class Float
def floor2(exp = 0)
multiplier = 10 ** exp
((self * multiplier).floor).to_f/multiplier.to_f
end
end
no_vat_price = 1.00
tva_rate = 20.0
previous_total = 1.05
loop do
tva_amount = (no_vat_price * tva_rate / 100).floor2(2)
difference = ((tva_amount + no_vat_price).round(2) - previous_total).floor2(2)
if difference > 0.01
puts "oops"
end
previous_total = (tva_amount + no_vat_price).round(2)
puts "novat : #{no_vat_price} tva_amount: #{tva_amount} total: #{previous_total} difference: #{difference}"
no_vat_price = (no_vat_price + 0.01).round(2)
break if no_vat_price > 100
end
@penso
Copy link
Author

penso commented Jan 24, 2018

oops
novat : 1.05 tva_amount: 0.21 total: 1.26
novat : 1.06 tva_amount: 0.21 total: 1.27
novat : 1.07 tva_amount: 0.21 total: 1.28
novat : 1.08 tva_amount: 0.21 total: 1.29
novat : 1.09 tva_amount: 0.21 total: 1.3
oops
novat : 1.1 tva_amount: 0.22 total: 1.32
novat : 1.11 tva_amount: 0.22 total: 1.33
novat : 1.12 tva_amount: 0.22 total: 1.34
novat : 1.13 tva_amount: 0.22 total: 1.35
novat : 1.14 tva_amount: 0.22 total: 1.36
novat : 1.15 tva_amount: 0.23 total: 1.38
novat : 1.16 tva_amount: 0.23 total: 1.39
novat : 1.17 tva_amount: 0.23 total: 1.4
novat : 1.18 tva_amount: 0.23 total: 1.41
novat : 1.19 tva_amount: 0.23 total: 1.42
oops
novat : 1.2 tva_amount: 0.24 total: 1.44
novat : 1.21 tva_amount: 0.24 total: 1.45
novat : 1.22 tva_amount: 0.24 total: 1.46
novat : 1.23 tva_amount: 0.24 total: 1.47
novat : 1.24 tva_amount: 0.24 total: 1.48
oops
novat : 1.25 tva_amount: 0.25 total: 1.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment