Ejemplo n.º 1
0
// coinUnits converts a siacoin amount to base units.
func coinUnits(amount string) (string, error) {
	units := []string{"pS", "nS", "uS", "mS", "SC", "KS", "MS", "GS", "TS"}
	for i, unit := range units {
		if strings.HasSuffix(amount, unit) {
			// scan into big.Rat
			r, ok := new(big.Rat).SetString(strings.TrimSuffix(amount, unit))
			if !ok {
				return "", errors.New("malformed amount")
			}
			// convert units
			exp := 24 + 3*(int64(i)-4)
			mag := new(big.Int).Exp(big.NewInt(10), big.NewInt(exp), nil)
			r.Mul(r, new(big.Rat).SetInt(mag))
			// r must be an integer at this point
			if !r.IsInt() {
				return "", errors.New("non-integer number of hastings")
			}
			return r.RatString(), nil
		}
	}
	// check for hastings separately
	if strings.HasSuffix(amount, "H") {
		return strings.TrimSuffix(amount, "H"), nil
	}

	return "", errors.New("amount is missing units; run 'wallet --help' for a list of units")
}
Ejemplo n.º 2
0
// parseFilesize converts strings of form 10GB to a size in bytes. Fractional
// sizes are truncated at the byte size.
func parseFilesize(strSize string) (string, error) {
	units := []struct {
		suffix     string
		multiplier int64
	}{
		{"kb", 1e3},
		{"mb", 1e6},
		{"gb", 1e9},
		{"tb", 1e12},
		{"kib", 1 << 10},
		{"mib", 1 << 20},
		{"gib", 1 << 30},
		{"tib", 1 << 40},
		{"b", 1}, // must be after others else it'll match on them all
		{"", 1},  // no suffix is still a valid suffix
	}

	strSize = strings.ToLower(strSize)
	for _, unit := range units {
		if strings.HasSuffix(strSize, unit.suffix) {
			r, ok := new(big.Rat).SetString(strings.TrimSuffix(strSize, unit.suffix))
			if !ok {
				return "", errUnableToParseSize
			}
			r.Mul(r, new(big.Rat).SetInt(big.NewInt(unit.multiplier)))
			if !r.IsInt() {
				f, _ := r.Float64()
				return fmt.Sprintf("%d", int64(f)), nil
			}
			return r.RatString(), nil
		}
	}

	return "", errUnableToParseSize
}
Ejemplo n.º 3
0
Archivo: lex.go Proyecto: suzuken/calc
// String creates string of big.Rat. This is output format of calc.
func String(r *big.Rat) string {
	return r.RatString()
}
Ejemplo n.º 4
0
Archivo: order.go Proyecto: sdvdxl/wine
// 上传订单
func OrderUploadHandler(c *gin.Context) {

	//销售不允许上传
	auth := userauth.Auth(c)
	if auth.IsRole(bean.ROLE_SALES) {
		c.HTML(http.StatusOK, "order_upload.tmpl", gin.H{"successCount": 0, "isUpload": true, "errMsg": "您没有权限"})
		return
	}

	request := c.Request
	request.ParseMultipartForm(2 << 10)
	file, fileHeader, err := request.FormFile("file")
	if err != nil {
		Logger.Error("upload file error, %v", err)
		panic(err)
	}

	_ = fileHeader

	csvReader := csv.NewReader(file)

	sql := insertSQL
	values := make([]interface{}, 0, 40)

	_, err = csvReader.Read()
	if err != nil {
		panic(err)
	} else if err == io.EOF {
		Logger.Debug("read file end")
		return
	}

	rowsCount := 0
	for {
		if row, err := csvReader.Read(); err != nil && err == io.EOF {
			break
		} else if err != io.EOF {
			rowsCount++
			// 49 params  47 ?
			sql += `(?, now(), now(), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
			?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ),`
			values = append(values, uuid.New())
			r := new(big.Rat)
			r.SetString(row[0])
			values = append(values, r.RatString())
			values = append(values, appendValues(row, 1, 12)...)
			Logger.Debug("address: %v", row[13])
			address := strings.Fields(row[13]) //dizhi

			Logger.Debug("address args len:%v", len(address))
			if len(address) == 4 {
				values = append(values, address[0])
				values = append(values, address[1])
				values = append(values, address[2])
				values = append(values, address[3])
				for i := 0; i < 3; i++ {
					values = append(values, nil)
				}
			} else {
				for i := 0; i < 7; i++ {
					values = append(values, nil)
				}
			}

			values = append(values, appendValues(row, 14, 15)...)

			runes := []rune(row[16])
			if strings.HasPrefix(row[16], "'") {
				runes = runes[1:]
			}
			values = append(values, string(runes))

			values = append(values, appendValues(row, 17, 28)...)

			runes = []rune(row[29])
			if strings.HasSuffix(row[29], "元") {
				runes = runes[:len(runes)-1]
			}
			buyerServiceFee, err := strconv.Atoi(string(runes))
			if err != nil {
				buyerServiceFee = 0
			}
			values = append(values, buyerServiceFee)
			values = append(values, appendValues(row, 30, 32)...)

			address = strings.Fields(row[35]) //dizhirow[36] //fixed dizhi
			Logger.Debug("fixed address: %v", row[35])
			Logger.Debug("fixed address args len:%v", len(address))
			// 7
			if len(address) == 4 {
				values = append(values, address[0])
				values = append(values, address[1])
				values = append(values, address[2])
				values = append(values, address[3])
				for i := 0; i < 3; i++ {
					values = append(values, nil)
				}
			} else {
				for i := 0; i < 7; i++ {
					values = append(values, nil)
				}
			}
		}
	}

	sql = sql[:len(sql)-1]

	Logger.Debug("sql:%v", sql)
	Logger.Debug("values len:%v", len(values))
	Logger.Debug("values:%v", values)

	if len(values) > 0 {
		stmt, err := db.Engine.DB().Prepare(sql)
		util.PanicError(err)
		defer stmt.Close()
		result, err := stmt.Exec(values...)
		util.PanicError(err)

		count, err := result.RowsAffected()
		util.PanicError(err)
		Logger.Debug("inserted rows count:%v", count)
	}

	c.HTML(http.StatusOK, "order_upload.tmpl", gin.H{"successCount": rowsCount, "isUpload": true, "errMsg": "上传成功"})

}