示例#1
0
func TestNumberFmtNumber2(t *testing.T) {
	// only to test the default format
	tests := []struct {
		opts    []i18n.NumberOptFunc
		sign    int
		i       int64
		prec    int
		frac    int64
		want    string
		wantErr error
	}{
		{
			[]i18n.NumberOptFunc{
				i18n.NumberFormat(""),
				i18n.NumberSymbols(testDefCurSym),
			},
			-1, -1234, 3, 6, "-1,234.006", nil, // euros with default Symbols
		},
		{
			[]i18n.NumberOptFunc{
				i18n.NumberFormat(""),
				i18n.NumberSymbols(testDefCurSym),
			},
			-1, -1234, 4, 6, "-1,234.001", nil, // euros with default Symbols
		},
	}

	var buf bytes.Buffer
	for _, test := range tests {
		haveNumber := i18n.NewNumber(test.opts...)

		_, err := haveNumber.FmtNumber(&buf, test.sign, test.i, test.prec, test.frac)
		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()
	}
}
示例#2
0
	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"},
		{1000, 13, "$ 0.01"},
		{100, -13, "$ -0.13"},
		{0, -45628734653, "$ -45,628,734,653.00"},
		{10, -45628734653, "$ -4,562,873,465.30"},