Skip to content

mvfavila/dinheiro

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

dinheiro

A Go library for handling Brazilian monetary values.

Installation

go get github.com/mvfavila/dinheiro

Overview

Values are represented as int64 centavos internally. Both functions accept equivalent inputs:

int64 (centavos) string (raw centavos) string (formatted)
2 "2" "0,02"
199 "199" "1,99"
100150 "100150" "1001,50"
100137 "100137" "1.001,37"

Negative values and unsupported types return an error.

Functions

ToText(value any) (string, error)

Formats a monetary value as a Brazilian currency string (dots as thousand separators, comma as decimal separator).

dinheiro.ToText(int64(2))          // "0,02"
dinheiro.ToText("30")              // "0,30"
dinheiro.ToText("1,99")            // "1,99"
dinheiro.ToText(int64(100137))     // "1.001,37"
dinheiro.ToText("7700022280")      // "77.000.222,80"
dinheiro.ToText("77000222,80")     // "77.000.222,80"
dinheiro.ToText("77.000.222,80")   // "77.000.222,80"
dinheiro.ToText("77.000.222")      // error

ToMoneyDescription(value any) (string, error)

Formats a monetary value as a written-out Brazilian Portuguese description.

dinheiro.ToMoneyDescription(int64(2))         // "dois centavos"
dinheiro.ToMoneyDescription("30")             // "trinta centavos"
dinheiro.ToMoneyDescription("1,99")           // "um real e noventa e nove centavos"
dinheiro.ToMoneyDescription(int64(100137))    // "um mil e um reais e trinta e sete centavos"
dinheiro.ToMoneyDescription("7700022280")     // "setenta e sete milhões duzentos e vinte e dois reais e oitenta centavos"
dinheiro.ToMoneyDescription("77000222,80")    // "setenta e sete milhões duzentos e vinte e dois reais e oitenta centavos"
dinheiro.ToMoneyDescription("77.000.222,80")  // "setenta e sete milhões duzentos e vinte e dois reais e oitenta centavos"
dinheiro.ToMoneyDescription("77.000.222")     // error

ToTextDescription(value any) (string, error)

Formats a monetary value as a written-out Brazilian Portuguese description without currency nouns.

dinheiro.ToTextDescription(int64(2))         // "dois décimos"
dinheiro.ToTextDescription("30")             // "trinta décimos"
dinheiro.ToTextDescription("1,99")           // "um e noventa e nove décimos"
dinheiro.ToTextDescription(int64(100137))    // "um mil e um e trinta e sete décimos"
dinheiro.ToTextDescription("7700022280")     // "setenta e sete milhões duzentos e vinte e dois e oitenta décimos"
dinheiro.ToTextDescription("77000222,80")    // "setenta e sete milhões duzentos e vinte e dois e oitenta décimos"
dinheiro.ToTextDescription("77.000.222,80")  // "setenta e sete milhões duzentos e vinte e dois e oitenta décimos"
dinheiro.ToTextDescription("77.000.222")     // error

Supported range

Any non-negative int64 value (up to ~92 quadrillion).

License

See LICENSE.

About

A Go library for handling Brazilian monetary values.

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages