github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/unit/area.go (about)

     1  // Code generated by "go generate github.com/jingcheng-WU/gonum/unit”; DO NOT EDIT.
     2  
     3  // Copyright ©2014 The Gonum Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  package unit
     8  
     9  import (
    10  	"errors"
    11  	"fmt"
    12  	"math"
    13  	"unicode/utf8"
    14  )
    15  
    16  // Area represents an area in square metres.
    17  type Area float64
    18  
    19  // Unit converts the Area to a *Unit.
    20  func (a Area) Unit() *Unit {
    21  	return New(float64(a), Dimensions{
    22  		LengthDim: 2,
    23  	})
    24  }
    25  
    26  // Area allows Area to implement a Areaer interface.
    27  func (a Area) Area() Area {
    28  	return a
    29  }
    30  
    31  // From converts the unit into the receiver. From returns an
    32  // error if there is a mismatch in dimension.
    33  func (a *Area) From(u Uniter) error {
    34  	if !DimensionsMatch(u, Area(0)) {
    35  		*a = Area(math.NaN())
    36  		return errors.New("unit: dimension mismatch")
    37  	}
    38  	*a = Area(u.Unit().Value())
    39  	return nil
    40  }
    41  
    42  func (a Area) Format(fs fmt.State, c rune) {
    43  	switch c {
    44  	case 'v':
    45  		if fs.Flag('#') {
    46  			fmt.Fprintf(fs, "%T(%v)", a, float64(a))
    47  			return
    48  		}
    49  		fallthrough
    50  	case 'e', 'E', 'f', 'F', 'g', 'G':
    51  		p, pOk := fs.Precision()
    52  		w, wOk := fs.Width()
    53  		const unit = " m^2"
    54  		switch {
    55  		case pOk && wOk:
    56  			fmt.Fprintf(fs, "%*.*"+string(c), pos(w-utf8.RuneCount([]byte(unit))), p, float64(a))
    57  		case pOk:
    58  			fmt.Fprintf(fs, "%.*"+string(c), p, float64(a))
    59  		case wOk:
    60  			fmt.Fprintf(fs, "%*"+string(c), pos(w-utf8.RuneCount([]byte(unit))), float64(a))
    61  		default:
    62  			fmt.Fprintf(fs, "%"+string(c), float64(a))
    63  		}
    64  		fmt.Fprint(fs, unit)
    65  	default:
    66  		fmt.Fprintf(fs, "%%!%c(%T=%g m^2)", c, a, float64(a))
    67  	}
    68  }