Increasing trades.price decimal scale (#89)

* Changing trades.price to have a larger scale - a scale of 4 causes destructive rounding when calculating transaction cost; changes to the UI to allow for inputting and showing increased scale trade prices; test case
This commit is contained in:
Michael Studman
2025-10-22 03:22:24 +11:00
committed by GitHub
parent 3aea1513d1
commit ea7ce13a7d
6 changed files with 50 additions and 5 deletions

View File

@@ -20,4 +20,38 @@ class TradeTest < ActiveSupport::TestCase
name = Trade.build_name("buy", 0.25, "BTC")
assert_equal "Buy 0.25 shares of BTC", name
end
test "price scale is preserved at 10 decimal places" do
security = Security.create!(ticker: "TEST", exchange_operating_mic: "XNAS")
# up to 10 decimal places — should persist exactly
precise_price = BigDecimal("12.3456789012")
trade = Trade.create!(
security: security,
price: precise_price,
qty: 10000,
currency: "USD"
)
trade.reload
assert_equal precise_price, trade.price
end
test "price is rounded to 10 decimal places" do
security = Security.create!(ticker: "TEST", exchange_operating_mic: "XNAS")
# over 10 decimal places — will be rounded
price_with_too_many_decimals = BigDecimal("1.123456789012345")
trade = Trade.create!(
security: security,
price: price_with_too_many_decimals,
qty: 1,
currency: "USD"
)
trade.reload
assert_equal BigDecimal("1.1234567890"), trade.price
end
end