當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。