示例#1
0
文件: main.go 项目: jvillasante/goeg
func processRequest(request *http.Request) ([3]float64, string, bool) {
	var floats [3]float64
	count := 0
	for index, key := range []string{"a", "b", "c"} {
		if slice, found := request.Form[key]; found && len(slice) > 0 {
			if slice[0] != "" {
				x, err := strconv.ParseFloat(slice[0], 64)
				if err != nil {
					return floats, "'" + slice[0] + "' is invalid", false
				}
				floats[index] = x
			} else { // as a courtesy to users treat blanks as 0
				request.Form[key][0] = "0"
				floats[index] = 0
			}
			count++
		}
	}
	if count != 3 { // the first time the form is empty;
		return floats, "", false // this isn't an error but there's
	} // nothing to calculate
	if numbers.EqualFloat(floats[0], 0, -1) {
		return floats, "the x² factor may not be 0", false
	}
	return floats, "", true
}
示例#2
0
文件: main.go 项目: jvillasante/goeg
func formatComplex(x complex128) string {
	if numbers.EqualFloat(imag(x), 0, -1) {
		return fmt.Sprintf("%.*f", decimals, real(x))
	}
	return fmt.Sprintf("%.*f", decimals, x)
}