golang货币金额格式化处理插件库accounting的使用

Golang货币金额格式化处理插件库accounting的使用

accounting是一个用于货币和金额格式化的Golang库,灵感来源于accounting.js。

快速开始

安装库:

go get github.com/leekchan/accounting

基本使用示例

package main

import (
    "fmt"
    "math/big"

    "github.com/shopspring/decimal"
    "github.com/leekchan/accounting"
)

func main() {
    // 基本格式化示例
    ac := accounting.Accounting{Symbol: "$", Precision: 2}
    fmt.Println(ac.FormatMoney(123456789.213123))                       // "$123,456,789.21"
    fmt.Println(ac.FormatMoney(12345678))                               // "$12,345,678.00"
    fmt.Println(ac.FormatMoney(big.NewRat(77777777, 3)))                // "$25,925,925.67"
    fmt.Println(ac.FormatMoney(big.NewRat(-77777777, 3)))               // "-$25,925,925.67"
    fmt.Println(ac.FormatMoneyBigFloat(big.NewFloat(123456789.213123))) // "$123,456,789.21"
    fmt.Println(ac.FormatMoneyDecimal(decimal.New(123456789.213123, 0))) // "$123,456,789.21"

    // 欧洲格式
    ac = accounting.Accounting{Symbol: "€", Precision: 2, Thousand: ".", Decimal: ","}
    fmt.Println(ac.FormatMoney(4999.99))  // "€4.999,99"

    // 从Locale结构获取货币信息
    lc := LocaleInfo["USD"]
    ac = accounting.Accounting{Symbol: lc.ComSymbol, Precision: 2, Thousand: lc.ThouSep, Decimal: lc.DecSep}
    fmt.Println(ac.FormatMoney(500000)) // "$500,000.00"

    // 英镑格式,无小数
    ac = accounting.Accounting{Symbol: "£ ", Precision: 0}
    fmt.Println(ac.FormatMoney(500000)) // "£ 500,000"

    // 自定义格式化
    ac = accounting.Accounting{Symbol: "GBP", Precision: 0,
        Format: "%s %v", FormatNegative: "%s (%v)", FormatZero: "%s --"}
    fmt.Println(ac.FormatMoney(1000000)) // "GBP 1,000,000"
    fmt.Println(ac.FormatMoney(-5000))   // "GBP (5,000)"
    fmt.Println(ac.FormatMoney(0))       // "GBP --"
}

Accounting结构

type Accounting struct {
    Symbol         string // 货币符号(必需)
    Precision      int    // 小数位数(可选/默认:0)
    Thousand       string // 千位分隔符(可选/默认:,)
    Decimal        string // 小数分隔符(可选/默认:.)
    Format         string // 简单格式字符串(%v=值,%s=符号)(默认:%s%v)
    FormatNegative string // 负值格式字符串
    FormatZero     string // 零值格式字符串
}

格式化方法

  1. FormatMoney(value interface{}) string - 支持多种类型的值
  2. *FormatMoneyBigFloat(value big.Float) string - 仅支持big.Float
  3. FormatMoneyInt(value int) string - 仅支持int
  4. *FormatMoneyBigRat(value big.Rat) string - 仅支持big.Rat
  5. FormatMoneyFloat64(value float64) string - 仅支持float64

数字格式化方法

  1. FormatNumber(value interface{}, precision int, thousand string, decimal string) string
  2. *FormatNumberBigFloat(value big.Float, precision int, thousand string, decimal string) string
  3. FormatNumberInt(value int, precision int, thousand string, decimal string) string
  4. *FormatNumberBigRat(value big.Rat, precision int, thousand string, decimal string) string
  5. FormatNumberFloat64(value float64, precision int, thousand string, decimal string) string

反向格式化

fmt.Println(accounting.UnformatNumber("$45,000.50", 2, "USD")) // "45000.50"
fmt.Println(accounting.UnformatNumber("EUR 12.500,3474", 3, "EUR")) // "12500.347"

注意事项

请不要使用float64来计算货币金额。在进行操作时,浮点数可能会有误差。建议使用big.Rat(Go < 1.5)或big.Float(Go >= 1.5)。


更多关于golang货币金额格式化处理插件库accounting的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html

1 回复

更多关于golang货币金额格式化处理插件库accounting的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


Golang货币金额格式化处理插件库accounting的使用

accounting是一个用于Golang的货币金额格式化库,它可以帮助开发者轻松处理货币金额的显示格式,包括千分位分隔符、小数点处理、货币符号等。

安装accounting库

go get github.com/leekchan/accounting

基本使用方法

1. 简单格式化

package main

import (
	"fmt"
	"github.com/leekchan/accounting"
)

func main() {
	ac := accounting.Accounting{Symbol: "$", Precision: 2}
	fmt.Println(ac.FormatMoney(123456789.213123)) // 输出: $123,456,789.21
}

2. 自定义格式化选项

accounting库提供了多种可配置选项:

ac := accounting.Accounting{
    Symbol:    "¥",       // 货币符号
    Precision: 2,         // 小数位数
    Thousand:  ",",       // 千分位分隔符
    Decimal:   ".",       // 小数点符号
    Format:    "%s %v",   // 输出格式: 符号 金额
    FormatNegative: "%s (%v)", // 负数格式
    FormatZero: "%s --",  // 零值格式
}

3. 格式化示例

package main

import (
	"fmt"
	"github.com/leekchan/accounting"
)

func main() {
	// 美元格式
	usd := accounting.Accounting{Symbol: "$", Precision: 2}
	fmt.Println(usd.FormatMoney(123456789.213123)) // $123,456,789.21
	
	// 人民币格式
	cny := accounting.Accounting{
		Symbol:    "¥",
		Precision: 2,
		Thousand:  ",",
		Decimal:   ".",
	}
	fmt.Println(cny.FormatMoney(123456789.213123)) // ¥123,456,789.21
	
	// 欧元格式
	eur := accounting.Accounting{
		Symbol:    "€",
		Precision: 2,
		Thousand:  ".",
		Decimal:   ",",
		Format:    "%v %s",
	}
	fmt.Println(eur.FormatMoney(123456789.213123)) // 123.456.789,21 €
	
	// 处理负数
	ac := accounting.Accounting{Symbol: "$", Precision: 2}
	fmt.Println(ac.FormatMoney(-123456.78)) // $-123,456.78
	
	// 自定义负数格式
	acNegative := accounting.Accounting{
		Symbol:          "$",
		Precision:       2,
		FormatNegative: "( %s%v )",
	}
	fmt.Println(acNegative.FormatMoney(-123456.78)) // ( $123,456.78 )
	
	// 处理零值
	acZero := accounting.Accounting{
		Symbol:    "$",
		Precision: 2,
		FormatZero: "%s 0.00",
	}
	fmt.Println(acZero.FormatMoney(0)) // $ 0.00
}

4. 格式化整数

ac := accounting.Accounting{Symbol: "$", Precision: 0}
fmt.Println(ac.FormatMoney(123456789)) // $123,456,789

5. 格式化大数字

ac := accounting.Accounting{Symbol: "", Precision: 2, Thousand: "'"}
fmt.Println(ac.FormatMoney(123456789.213123)) // 123'456'789.21

6. 批量格式化

ac := accounting.Accounting{Symbol: "€", Precision: 2, Thousand: ".", Decimal: ","}
numbers := []float64{1234.56, -9876.54, 0, 1234567.89}
for _, num := range numbers {
    fmt.Println(ac.FormatMoney(num))
}
// 输出:
// €1.234,56
// €-9.876,54
// €0,00
// €1.234.567,89

高级功能

1. 自定义格式化函数

ac := accounting.Accounting{
    Format: func(symbol string, value string) string {
        return fmt.Sprintf("[%s %s]", symbol, value)
    },
    Symbol: "¥",
    Precision: 2,
}
fmt.Println(ac.FormatMoney(123456.78)) // [¥ 123,456.78]

2. 解析格式化后的金额

ac := accounting.Accounting{Symbol: "$", Precision: 2}
formatted := ac.FormatMoney(123456.78)
fmt.Println(formatted) // $123,456.78

// 如果需要反向解析,可以自己实现解析函数

注意事项

  1. accounting库主要用于显示格式化,不适用于精确的货币计算
  2. 对于金融应用,建议使用专门的decimal库进行精确计算后再用accounting格式化显示
  3. 不同地区的货币格式差异较大,需要根据目标用户设置合适的格式

accounting库提供了一种简单直观的方式来格式化货币金额,特别适合需要显示金额的Web应用或报表系统。通过灵活的配置选项,可以满足大多数国际化货币显示需求。

回到顶部