Created
October 31, 2014 05:47
-
-
Save cesare/915a476c3035bf6a8ee0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TAX_RATE = 1.08 | |
# 「1.08 を掛けて→小数点以下を切り捨てる」という演算で target を超えない最大の数値を求める | |
# 戻値 [t: Fixnum, d: Fixnum, i: Fixnum, s: Fixnum] | |
# | |
# t: 元々の税込み価格 (= target) | |
# d: 求める税抜き価格 | |
# i: 計算結果の税抜き価格に税を乗せた価格 | |
# s: 差分 (t - i) | |
# | |
def maximum_tax_excluding_price(target) | |
lower_limit = (target / TAX_RATE).floor | |
upper_limit = (target / TAX_RATE).ceil | |
(lower_limit..upper_limit) | |
.map {|d| i = (d * TAX_RATE).truncate; [target, d, i, target - i] } | |
.select {|t, d, i, s| i <= target } | |
.max_by {|t, d, i, s| d } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment