Example #1
0
	Group:                  0,
	List:                   ';',
	PercentSign:            '%',
	CurrencySign:           '¤',
	PlusSign:               '+',
	MinusSign:              '-',
	Exponential:            'E',
	SuperscriptingExponent: '×',
	PerMille:               '‰',
	Infinity:               '∞',
	Nan:                    []byte(`NaN`),
}

var testFmtCur = i18n.NewCurrency(
	i18n.CurrencyFormat("¤ #,##0.00"),
	i18n.CurrencySymbols(testDefaultSymbols),
)
var testFmtNum = i18n.NewNumber(
	i18n.NumberFormat("###0.###"),
	i18n.NumberSymbols(testDefaultSymbols),
)

func TestString(t *testing.T) {
	tests := []struct {
		prec int
		have int64
		want string
	}{
		{0, 13, "$ 13.00"},
		{10, 13, "$ 1.30"},
		{100, 13, "$ 0.13"},
Example #2
0
func TestFmtCurrency2(t *testing.T) {

	tests := []struct {
		opts    []i18n.CurrencyOptFunc
		sign    int
		i       int64
		prec    int
		dec     int64
		want    string
		wantErr error
	}{
		{
			[]i18n.CurrencyOptFunc{
				i18n.CurrencyFormat("#,##0.00 ¤"),
				i18n.CurrencyFraction(2, 0, 2, 0), // euro, 2 digits, no rounding
				i18n.CurrencySign([]byte("€")),
			},
			-1, -1234, 2, 6, "-1.234,06 €", nil, // euros with default Symbols
		},
		{
			[]i18n.CurrencyOptFunc{
				i18n.CurrencyFormat("#,##0.00 ¤"),
				i18n.CurrencyFraction(2, 0, 2, 0), // euro, 2 digits, no rounding
				i18n.CurrencySign([]byte("€")),
			},
			-1, -1234, 1, 6, "-1.234,60 €", nil, // euros with default Symbols
		},
		{
			[]i18n.CurrencyOptFunc{i18n.CurrencyFormat("+#,##0.00 ¤"), i18n.CurrencyFraction(2, 0, 2, 0), i18n.CurrencySign([]byte("€"))},
			0, 1234, 2, 6, "+1.234,06 €", nil, // number is 1234.06
		},
		{
			[]i18n.CurrencyOptFunc{i18n.CurrencyFormat("#,##0. ¤"), i18n.CurrencyFraction(2, 0, 2, 0), i18n.CurrencySign([]byte("€"))},
			1, 1234, 2, 6, "1.234,06 €", nil, // number is 1234.06
		},
		{
			[]i18n.CurrencyOptFunc{i18n.CurrencyFormat("#,##0. ¤"), i18n.CurrencyFraction(2, 0, 2, 0), i18n.CurrencySign([]byte("€"))},
			1, 1234, 3, 6, "1.234,01 €", nil, // number is 1234.006
		},
		{
			[]i18n.CurrencyOptFunc{i18n.CurrencyFormat("#,##0. ¤"), i18n.CurrencyFraction(2, 0, 2, 0), i18n.CurrencySign([]byte("€"))},
			1, 1234, 3, 345, "1.234,35 €", nil, // number is 1234.345
		},
		{
			[]i18n.CurrencyOptFunc{i18n.CurrencyFormat("#,##0. ¤"), i18n.CurrencyFraction(2, 0, 2, 0), i18n.CurrencySign([]byte("€"))},
			1, 1234, 3, 45, "1.234,05 €", nil, // number is 1234.045
		},
		{
			[]i18n.CurrencyOptFunc{i18n.CurrencyFormat("#,##0. ¤"), i18n.CurrencyFraction(0, 0, 2, 0), i18n.CurrencySign([]byte("€"))},
			1, 123456789, 1, 6, "123.456.790 €", nil, // 123456789.6
		},
		{
			[]i18n.CurrencyOptFunc{i18n.CurrencyFormat("#,##0. ¤"), i18n.CurrencyFraction(0, 0, 2, 0), i18n.CurrencySign([]byte("€"))},
			1, 123456789, 2, 6, "123.456.789 €", nil, // 123456789.06
		},
		{
			[]i18n.CurrencyOptFunc{i18n.CurrencyFormat("+0.00 ¤"), i18n.CurrencyFraction(2, 0, 2, 0), i18n.CurrencySign([]byte("€"))},
			-1, 4, 3, 245, "+4,25 €", nil, // 4.245
		},
		{
			[]i18n.CurrencyOptFunc{i18n.CurrencyFormat("¤\u00a0#0.00;¤\u00a0-#0.00"), i18n.CurrencyFraction(2, 0, 2, 0), i18n.CurrencySign([]byte("€"))},
			-1, 4, 2, 245, "€\u00a04,25", i18n.ErrPrecIsTooShort, // 4.245
		},
		{
			[]i18n.CurrencyOptFunc{i18n.CurrencyFormat("¤\u00a0#0.00;¤\u00a0-#0.00"), i18n.CurrencyFraction(2, 0, 2, 0), i18n.CurrencySign([]byte("€"))},
			-1, -12345678, 3, 245, "€\u00a0—12345678,25", nil, // 12345678.245
		},
		{
			[]i18n.CurrencyOptFunc{i18n.CurrencyFormat("¤\u00a0#0.00;¤\u00a0-#0.00"), i18n.CurrencyFraction(-1, -1, -1, -1), i18n.CurrencySign([]byte(""))},
			-1, -12345678, 3, 245, "\uf8ff\u00a0-12345678", nil, // 12345678.245
		},
		{
			[]i18n.CurrencyOptFunc{i18n.CurrencyFormat("¤\u00a0#0.00;¤\u00a0-#0.00"), i18n.CurrencyFraction(0, 0, 2, 0), i18n.CurrencySign([]byte(""))},
			-1, -12345678, 3, 495, "\uf8ff\u00a0-12345679", nil, // 12345678.495
		},
		{
			[]i18n.CurrencyOptFunc{i18n.CurrencyFormat("#,##0.00¤;(#,##0.00¤)"), i18n.CurrencyFraction(2, 0, 2, 0), i18n.CurrencySign([]byte("C"))},
			-1, -12345678, 3, 495, "(12.345.678,50C)", nil, // 12345678.495
		},
		{
			[]i18n.CurrencyOptFunc{i18n.CurrencyFormat("#,##0.00¤;(#,##0.00¤)"), i18n.CurrencyFraction(2, 0, 2, 0), i18n.CurrencySign([]byte("C"))},
			0, 0, 3, 495, "", i18n.ErrCannotDetectMinusSign, // 0.495
		},
		{
			[]i18n.CurrencyOptFunc{i18n.CurrencyFormat("#,##0.00¤;(#,##0.00¤)"), i18n.CurrencyFraction(2, 0, 2, 0), i18n.CurrencySign([]byte("C"))},
			-1, 0, 3, 495, "(0,50C)", nil, // 0.495
		},
		{
			[]i18n.CurrencyOptFunc{
				i18n.CurrencyFormat("#,##0.00 ¤"),
				i18n.CurrencyFraction(0, 0, 0, 0), // japanese yen, no digits no rounding
				i18n.CurrencySign([]byte("¥JP")),
			},
			1, 1234, 3, 456, "1.235 ¥JP", nil, // yen with default symbols
		},
	}
	var buf bytes.Buffer
	for _, test := range tests {
		haveNumber := i18n.NewCurrency(i18n.CurrencySymbols(testDefCurSym))
		haveNumber.COptions(test.opts...)

		_, err := haveNumber.FmtNumber(&buf, test.sign, test.i, test.prec, test.dec)
		have := buf.String()
		if test.wantErr != nil {
			assert.Error(t, err)
			assert.EqualError(t, err, test.wantErr.Error())
		} else {
			assert.NoError(t, err)

			assert.EqualValues(t, test.want, have, "%v", test)
		}
		buf.Reset()
	}
}