Currency type with basic algebra and pretty printing

What is the go-to package for converting floating point numbers into currencies that can be added and subtracted and pretty printed on tables?

Found Currencies.jl, CurrencyAmounts.jl and Assets.jl but none of them seem to have what I need. I basically need to convert a vector of floating point into currencies for pretty printing:

julia> BRL.([1, 5, 10])
R$1,00
R$5.00
R$10.00

Try this

using Printf

function fp2currency(n)
      return "\$" * rsplit(@sprintf("%-12.2f",n))[1]
end

You can use it as

julia> fp2currency(12.34) |> println
$12.34

julia> fp2currency.([1, 5, 10])
3-element Vector{String}:
 "\$1.00"
 "\$5.00"
 "\$10.00"

You cannot add or subtract. You should only do this when you want to print it out.

If what you want is Currency then you could just use Unitful.jl

Have you checked GitHub - rmsrosa/UnitfulAssets.jl: A supplemental units package for Unitful.jl to handle assets.?

1 Like

Thank you @barucden I will take a look.