github.com/gopherd/gonum@v0.0.4/unit/charge.go (about)

     1  // Code generated by "go generate github.com/gopherd/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  // Charge represents an electric charge in Coulombs.
    17  type Charge float64
    18  
    19  const Coulomb Charge = 1
    20  
    21  // Unit converts the Charge to a *Unit.
    22  func (ch Charge) Unit() *Unit {
    23  	return New(float64(ch), Dimensions{
    24  		CurrentDim: 1,
    25  		TimeDim:    1,
    26  	})
    27  }
    28  
    29  // Charge allows Charge to implement a Charger interface.
    30  func (ch Charge) Charge() Charge {
    31  	return ch
    32  }
    33  
    34  // From converts the unit into the receiver. From returns an
    35  // error if there is a mismatch in dimension.
    36  func (ch *Charge) From(u Uniter) error {
    37  	if !DimensionsMatch(u, Coulomb) {
    38  		*ch = Charge(math.NaN())
    39  		return errors.New("unit: dimension mismatch")
    40  	}
    41  	*ch = Charge(u.Unit().Value())
    42  	return nil
    43  }
    44  
    45  func (ch Charge) Format(fs fmt.State, c rune) {
    46  	switch c {
    47  	case 'v':
    48  		if fs.Flag('#') {
    49  			fmt.Fprintf(fs, "%T(%v)", ch, float64(ch))
    50  			return
    51  		}
    52  		fallthrough
    53  	case 'e', 'E', 'f', 'F', 'g', 'G':
    54  		p, pOk := fs.Precision()
    55  		w, wOk := fs.Width()
    56  		const unit = " C"
    57  		switch {
    58  		case pOk && wOk:
    59  			fmt.Fprintf(fs, "%*.*"+string(c), pos(w-utf8.RuneCount([]byte(unit))), p, float64(ch))
    60  		case pOk:
    61  			fmt.Fprintf(fs, "%.*"+string(c), p, float64(ch))
    62  		case wOk:
    63  			fmt.Fprintf(fs, "%*"+string(c), pos(w-utf8.RuneCount([]byte(unit))), float64(ch))
    64  		default:
    65  			fmt.Fprintf(fs, "%"+string(c), float64(ch))
    66  		}
    67  		fmt.Fprint(fs, unit)
    68  	default:
    69  		fmt.Fprintf(fs, "%%!%c(%T=%g C)", c, ch, float64(ch))
    70  	}
    71  }