当前位置: 首页>>代码示例>>Golang>>正文


Golang decimal.NewFromFloat函数代码示例

本文整理汇总了Golang中github.com/shopspring/decimal.NewFromFloat函数的典型用法代码示例。如果您正苦于以下问题:Golang NewFromFloat函数的具体用法?Golang NewFromFloat怎么用?Golang NewFromFloat使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了NewFromFloat函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: BenchmarkDoCreateAccount

func BenchmarkDoCreateAccount(b *testing.B) {
	for n := 0; n < b.N; n++ {
		accountDetail := AccountDetails{
			"",
			"",
			"User,Test",
			decimal.NewFromFloat(0.),
			decimal.NewFromFloat(0.),
			decimal.NewFromFloat(0.),
			"cheque",
			0,
		}

		accountHolderDetail := AccountHolderDetails{
			"Test",
			"User",
			"1900-01-01",
			"19000101-1000-100",
			"555-123-1234",
			"",
			"[email protected]er.com",
			"Address 1",
			"Address 2",
			"Address 3",
			"22202",
		}

		ti := time.Now()
		sqlTime := int32(ti.Unix())
		_ = doCreateAccount(sqlTime, &accountDetail, &accountHolderDetail)
		_ = doDeleteAccount(&accountDetail)
	}
}
开发者ID:BVNK,项目名称:bank,代码行数:33,代码来源:database_test.go

示例2: setAccountDetails

func setAccountDetails(data []string) (accountDetails AccountDetails, err error) {
	if len(data) < 15 {
		return AccountDetails{}, errors.New("accounts.setAccountDetails: Not all fields required present")
	}

	if data[4] == "" {
		return AccountDetails{}, errors.New("accounts.setAccountDetails: Family name cannot be empty")
	}
	if data[3] == "" {
		return AccountDetails{}, errors.New("accounts.setAccountDetails: Given name cannot be empty")
	}
	accountDetails.BankNumber = BANK_NUMBER
	accountDetails.AccountHolderName = data[4] + "," + data[3] // Family Name, Given Name
	accountDetails.AccountBalance = decimal.NewFromFloat(OPENING_BALANCE)
	accountDetails.Overdraft = decimal.NewFromFloat(OPENING_OVERDRAFT)
	accountDetails.AvailableBalance = decimal.NewFromFloat(OPENING_BALANCE + OPENING_OVERDRAFT)
	// Get account type
	accountType := data[14]
	switch accountType {
	case "":
		accountType = "cheque" // Default to chequing account
		break
	case "savings", "cheque", "merchant", "money-market", "cd", "ira", "rcp", "credit", "mortgage", "loan":
		// Valid
		break
	default:
		return AccountDetails{}, errors.New("accounts.setAccountDetails: Account type not valid, must be one of savings, cheque, merchant, money-market, cd, ira, rcp, credit, mortgage, loan")
		break
	}
	accountDetails.Type = accountType

	return
}
开发者ID:BVNK,项目名称:bank,代码行数:33,代码来源:accounts.go

示例3: Encode

//Encode generates a geozip bucket id for a given latitude and longitude.
//Argument validate is true if a validation is to be performed.
//Precision is a number in the range of [0, 18] such that 0 gives lowest precision (000000000000000000) and 18 gives the most precise bucket id.
func Encode(latitude, longitude float64, validate bool, precision int) int64 {
	if validate && !Valid(latitude, longitude) {
		return 0
	}
	latitudeShifted := decimal.NewFromFloat(latitude).Add(decimal.NewFromFloat(90.0))
	longitudeShifted := decimal.NewFromFloat(longitude).Add(decimal.NewFromFloat(180.0))
	latString := latitudeShifted.String() + ".0"
	lonString := longitudeShifted.String() + ".0"
	latParts := strings.Split(latString, ".")
	lonParts := strings.Split(lonString, ".")
	latString = resizeCharacteristic(latParts[0]) + resizeMantissa(latParts[1])
	lonString = resizeCharacteristic(lonParts[0]) + resizeMantissa(lonParts[1])
	bucketString := zip(latString, lonString)
	bucket, err := strconv.ParseInt(bucketString, 10, 64)
	if err != nil {
		fmt.Errorf("Error parsing zipped string to int64")
	}
	for i := 0; i < maxPrecision-precision; i++ {
		bucket /= 10
	}
	for i := 0; i < maxPrecision-precision; i++ {
		bucket *= 10
	}
	return bucket
}
开发者ID:nick11roberts,项目名称:geozip,代码行数:28,代码来源:geozip.go

示例4: Money

func Money(value interface{}, code string) (MoneyObject, error) {
	currency, found := CurrencyTypes[code]
	if !found {
		return MoneyObject{}, errors.New("Code not found.")
	}

	var money decimal.Decimal
	var moneyObject MoneyObject

	switch v := value.(type) {
	case string:
		m, err := decimal.NewFromString(v)
		if err != nil {
			return MoneyObject{}, err
		}
		money = m
	case float32:
		money = decimal.NewFromFloat(float64(v))
	case float64:
		money = decimal.NewFromFloat(v)
	case int:
		money = decimal.NewFromFloat(float64(v))
	default:
		return MoneyObject{}, errors.New("Value could not be translated.")
	}

	moneyObject.money = money
	moneyObject.currency = currency

	return moneyObject, nil
}
开发者ID:stints,项目名称:gocurrency,代码行数:31,代码来源:currency.go

示例5: TestDoCreateAccount

func TestDoCreateAccount(t *testing.T) {
	//accountDetails AccountDetails, accountHolderDetails AccountHolderDetails
	accountDetail := AccountDetails{
		"",
		"",
		"User,Test",
		decimal.NewFromFloat(0.),
		decimal.NewFromFloat(0.),
		decimal.NewFromFloat(0.),
		0,
	}

	ti := time.Now()
	sqlTime := int32(ti.Unix())
	err := doCreateAccount(sqlTime, &accountDetail)

	if err != nil {
		t.Errorf("DoCreateAccount does not pass. Looking for %v, got %v", nil, err)
	}

	err = doDeleteAccount(&accountDetail)
	if err != nil {
		t.Errorf("DoDeleteAccount does not pass. Looking for %v, got %v", nil, err)
	}
}
开发者ID:thiesa,项目名称:bank,代码行数:25,代码来源:database_test.go

示例6: Decode

//Decode is the inverse operation of Encode.
//Decode returns latitude, longitude, and whether or not they are both represented precisely as float64 types.
func Decode(bucket int64) (float64, float64, bool) {
	var latitudeUnshifted, longitudeUnshifted decimal.Decimal
	var latitude, longitude float64
	var err error
	var exact bool
	bucketString := strconv.FormatInt(bucket, 10)
	for len(bucketString) < 18 {
		bucketString = "0" + bucketString
	}

	latString, lonString := unzip(bucketString)
	latString = latString[0:3] + "." + latString[3:]
	lonString = lonString[0:3] + "." + lonString[3:]

	latitudeUnshifted, err = decimal.NewFromString(latString)
	longitudeUnshifted, err = decimal.NewFromString(lonString)
	if err != nil {
		fmt.Errorf("Error creating decimal from string")
	}
	latitudeUnshifted = latitudeUnshifted.Sub(decimal.NewFromFloat(90.0))
	longitudeUnshifted = longitudeUnshifted.Sub(decimal.NewFromFloat(180.0))
	latitude, exact = latitudeUnshifted.Float64()
	longitude, exact = longitudeUnshifted.Float64()
	return latitude, longitude, exact
}
开发者ID:nick11roberts,项目名称:geozip,代码行数:27,代码来源:geozip.go

示例7: TestSquareRootOfFour

func TestSquareRootOfFour(t *testing.T) {
	four := decimal.NewFromFloat(4.0)
	sqrt := Sqrt(four)
	if !sqrt.Equals(decimal.NewFromFloat(2.0)) {
		t.Fatalf("Square root of %s is %s", four, sqrt)
	}
	t.Logf("Square root of %s is %s", four, sqrt)
}
开发者ID:fernandosanchezjr,项目名称:gotsp,代码行数:8,代码来源:sqrt_test.go

示例8: BenchmarkAddDecimalWithoutNew

func BenchmarkAddDecimalWithoutNew(b *testing.B) {
	decimalStartFloat := decimal.NewFromFloat(StartFloat)
	decimalFactor := decimal.NewFromFloat(Factor)

	for n := 0; n < b.N; n++ {
		AddDecimalWithoutNew(decimalStartFloat, decimalFactor)
	}
}
开发者ID:Zumata,项目名称:benchmark,代码行数:8,代码来源:decimal_test.go

示例9: AddDecimal

func AddDecimal() (calculated decimal.Decimal) {
	decimalStartFloat := decimal.NewFromFloat(StartFloat)
	decimalFactor := decimal.NewFromFloat(Factor)

	calculated = decimalStartFloat.Add(decimalFactor)

	return
}
开发者ID:Zumata,项目名称:benchmark,代码行数:8,代码来源:decimal.go

示例10: MultiplyDecimal

func MultiplyDecimal() (calculated decimal.Decimal) {
	decimalStartFloat := decimal.NewFromFloat(StartFloat)
	decimalFactor := decimal.NewFromFloat(Factor)

	calculated = decimalStartFloat.Mul(decimalFactor)

	return
}
开发者ID:Zumata,项目名称:benchmark,代码行数:8,代码来源:decimal.go

示例11: TestCanWidthdraw

func TestCanWidthdraw(t *testing.T) {

	account := CreateAccount(10)
	account.Deposit(decimal.NewFromFloat(20))

	account.Withdraw(decimal.NewFromFloat(50))
	assert.Equal(t, decimal.NewFromFloat(20).String(), account.GetBalance().String())

}
开发者ID:Hinton,项目名称:go-learn,代码行数:9,代码来源:account_test.go

示例12: Sqrt

// Square root using Newton's method
// Shamelessly ripped from https://gist.github.com/marcellodesales/2487009
func Sqrt(x decimal.Decimal) decimal.Decimal {
	p := decimal.NewFromFloat(0.0)
	z := decimal.NewFromFloat(1.0)
	for !p.Sub(z).Equals(ConstMin) {
		z = newton(z, x)
		p = newton(z, x)
	}
	return z
}
开发者ID:fernandosanchezjr,项目名称:gotsp,代码行数:11,代码来源:sqrt.go

示例13: TestCalculate

func TestCalculate(t *testing.T) {
	irrf := NewIRRF(irrfByYear["2016"])
	for _, testCase := range testCases {
		irrfDue := irrf.Calculate(decimal.NewFromFloat(testCase.irrfBase))
		if irrfDue.Cmp(decimal.NewFromFloat(testCase.irrfDue)) != 0 {
			t.Errorf("Expected %v, got %v", testCase.irrfDue, irrfDue)
		}
	}
}
开发者ID:thiagocaiubi,项目名称:isiro,代码行数:9,代码来源:irrf_test.go

示例14: TestCalculate

func TestCalculate(t *testing.T) {
	inss := NewINSS(inssByYear["2016"])
	for _, testCase := range testCases {
		inssDue := inss.Calculate(decimal.NewFromFloat(testCase.grossSalary))
		if inssDue.Cmp(decimal.NewFromFloat(testCase.inssDue)) != 0 {
			t.Errorf("Expected %v, got %v", testCase.inssDue, inssDue)
		}
	}
}
开发者ID:thiagocaiubi,项目名称:isiro,代码行数:9,代码来源:inss_test.go

示例15: setupGrid

func setupGrid() *Grid {
	grid := NewGrid(NewCoord(decimal.NewFromFloat(0.0),
		decimal.NewFromFloat(0.0)), decimal.NewFromFloat(90.0),
		decimal.NewFromFloat(180.0), nil, nil)
	grid.Append(coordA)
	grid.Append(coordB)
	grid.Append(coordC)
	grid.Append(coordD)
	return grid
}
开发者ID:fernandosanchezjr,项目名称:gotsp,代码行数:10,代码来源:grids_test.go


注:本文中的github.com/shopspring/decimal.NewFromFloat函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。