Skip to content

Commit

Permalink
Format currency values to 2 decimal places
Browse files Browse the repository at this point in the history
  • Loading branch information
mike76-dev committed May 7, 2024
1 parent 17ab51b commit daa06bd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 18 deletions.
24 changes: 24 additions & 0 deletions modules/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"math"
"math/big"
"strings"

"go.sia.tech/core/types"
)
Expand Down Expand Up @@ -64,6 +65,29 @@ func MulFloat(c types.Currency, f float64) types.Currency {
return num.Div(denom)
}

// ToString formats a types.Currency to a maximum of 2 decimal places.
func ToString(c types.Currency) string {
if c.IsZero() {
return "0 H"
}
s := c.Big().String()
u := (len(s) - 1) / 3
if u < 4 {
return s + " H"
} else if u > 12 {
u = 12
}
mant := s[:len(s)-u*3]
if frac := strings.TrimRight(s[len(s)-u*3:], "0"); len(frac) > 0 {
if len(frac) > 2 {
frac = frac[:2]
}
mant += "." + frac
}
unit := []string{"pS", "nS", "uS", "mS", "SC", "KS", "MS", "GS", "TS"}[u-4]
return mant + " " + unit
}

// Tax calculates the Siafund fee from the amount.
func Tax(height uint64, payout types.Currency) types.Currency {
// First 21,000 blocks need to be treated differently.
Expand Down
36 changes: 18 additions & 18 deletions modules/portal/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,24 +521,24 @@ func (api *portalAPI) getContracts(renter modules.Renter, current, old bool) []r
remainingCollateral := c.Transaction.FileContractRevisions[0].MissedProofOutputs[1].Value
contract := renterContract{
BadContract: c.Utility.BadContract,
DownloadSpending: c.DownloadSpending.String(),
DownloadSpending: modules.ToString(c.DownloadSpending),
EndHeight: c.EndHeight,
Fees: c.TxnFee.Add(c.SiafundFee).Add(c.ContractFee).String(),
FundAccountSpending: c.FundAccountSpending.String(),
Fees: modules.ToString(c.TxnFee.Add(c.SiafundFee).Add(c.ContractFee)),
FundAccountSpending: modules.ToString(c.FundAccountSpending),
GoodForUpload: c.Utility.GoodForUpload,
GoodForRenew: c.Utility.GoodForRenew,
HostPublicKey: c.HostPublicKey.String(),
HostVersion: hdbe.Settings.Version,
ID: c.ID.String(),
NetAddress: netAddress,
MaintenanceSpending: maintenanceSpending.String(),
RenterFunds: c.RenterFunds.String(),
RemainingCollateral: remainingCollateral.Sub(cp).String(),
MaintenanceSpending: modules.ToString(maintenanceSpending),
RenterFunds: modules.ToString(c.RenterFunds),
RemainingCollateral: modules.ToString(remainingCollateral.Sub(cp)),
Size: modules.FilesizeUnits(c.Size()),
StartHeight: c.StartHeight,
StorageSpending: c.StorageSpending.String(),
TotalCost: c.TotalCost.String(),
UploadSpending: c.UploadSpending.String(),
StorageSpending: modules.ToString(c.StorageSpending),
TotalCost: modules.ToString(c.TotalCost),
UploadSpending: modules.ToString(c.UploadSpending),
}

// Determine contract status.
Expand Down Expand Up @@ -592,24 +592,24 @@ func (api *portalAPI) getContracts(renter modules.Renter, current, old bool) []r
remainingCollateral := c.Transaction.FileContractRevisions[0].MissedProofOutputs[1].Value
contract := renterContract{
BadContract: c.Utility.BadContract,
DownloadSpending: c.DownloadSpending.String(),
DownloadSpending: modules.ToString(c.DownloadSpending),
EndHeight: c.EndHeight,
Fees: c.TxnFee.Add(c.SiafundFee).Add(c.ContractFee).String(),
FundAccountSpending: c.FundAccountSpending.String(),
Fees: modules.ToString(c.TxnFee.Add(c.SiafundFee).Add(c.ContractFee)),
FundAccountSpending: modules.ToString(c.FundAccountSpending),
GoodForUpload: c.Utility.GoodForUpload,
GoodForRenew: c.Utility.GoodForRenew,
HostPublicKey: c.HostPublicKey.String(),
HostVersion: hdbe.Settings.Version,
ID: c.ID.String(),
NetAddress: netAddress,
MaintenanceSpending: maintenanceSpending.String(),
RenterFunds: c.RenterFunds.String(),
RemainingCollateral: remainingCollateral.Sub(cp).String(),
MaintenanceSpending: modules.ToString(maintenanceSpending),
RenterFunds: modules.ToString(c.RenterFunds),
RemainingCollateral: modules.ToString(remainingCollateral.Sub(cp)),
Size: modules.FilesizeUnits(size),
StartHeight: c.StartHeight,
StorageSpending: c.StorageSpending.String(),
TotalCost: c.TotalCost.String(),
UploadSpending: c.UploadSpending.String(),
StorageSpending: modules.ToString(c.StorageSpending),
TotalCost: modules.ToString(c.TotalCost),
UploadSpending: modules.ToString(c.UploadSpending),
}

// Determine contract status.
Expand Down

0 comments on commit daa06bd

Please sign in to comment.