# Currency type with basic algebra and pretty printing

**URL:** <https://discourse.julialang.org/t/currency-type-with-basic-algebra-and-pretty-printing/95700>\
**Category:** Finance and Economics\
**Tags:** economics, finance, currency\
**Created:** [March 7, 2023, 10:36pm UTC](https://discourse.julialang.org/t/currency-type-with-basic-algebra-and-pretty-printing/95700 "2023-03-07T22:36:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [March 7, 2023, 10:36pm UTC](https://discourse.julialang.org/t/currency-type-with-basic-algebra-and-pretty-printing/95700/1 "2023-03-07T22:36:38Z")

</div>

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
julia> BRL.([1, 5, 10])
R$1,00
R$5.00
R$10.00
```

---

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [March 8, 2023, 8:20am UTC](https://discourse.julialang.org/t/currency-type-with-basic-algebra-and-pretty-printing/95700/2 "2023-03-08T08:20:47Z")

</div>

Try this

```julia
using Printf

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

```

You can use it as

```julia
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

---

<div class="post-metadata">

**Author:** ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)\
**Post date:** [March 8, 2023, 8:51am UTC](https://discourse.julialang.org/t/currency-type-with-basic-algebra-and-pretty-printing/95700/3 "2023-03-08T08:51:37Z")

</div>

Have you checked [GitHub - rmsrosa/UnitfulAssets.jl: A supplemental units package for Unitful.jl to handle assets.](https://github.com/rmsrosa/UnitfulAssets.jl)?

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [March 8, 2023, 4:54pm UTC](https://discourse.julialang.org/t/currency-type-with-basic-algebra-and-pretty-printing/95700/4 "2023-03-08T16:54:02Z")

</div>

Thank you @barucden I will take a look.
