github.com/goki/ki@v1.1.11/bools/bools.go (about)

     1  // Copyright (c) 2022, The GoKi Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  /*
     6  package bools does conversion to / from booleans and other go standard types
     7  */
     8  package bools
     9  
    10  // ToFloat32 converts a bool to a 1 (true) or 0 (false)
    11  func ToFloat32(b bool) float32 {
    12  	if b {
    13  		return 1
    14  	}
    15  	return 0
    16  }
    17  
    18  // ToFloat64 converts a bool to a 1 (true) or 0 (false)
    19  func ToFloat64(b bool) float64 {
    20  	if b {
    21  		return 1
    22  	}
    23  	return 0
    24  }
    25  
    26  // ToInt converts a bool to a 1 (true) or 0 (false)
    27  func ToInt(b bool) int {
    28  	if b {
    29  		return 1
    30  	}
    31  	return 0
    32  }
    33  
    34  // ToInt32 converts a bool to a 1 (true) or 0 (false)
    35  func ToInt32(b bool) int32 {
    36  	if b {
    37  		return 1
    38  	}
    39  	return 0
    40  }
    41  
    42  // ToInt64 converts a bool to a 1 (true) or 0 (false)
    43  func ToInt64(b bool) int64 {
    44  	if b {
    45  		return 1
    46  	}
    47  	return 0
    48  }
    49  
    50  // ToString converts a bool to "true" or "false" string
    51  func ToString(b bool) string {
    52  	if b {
    53  		return "true"
    54  	}
    55  	return "false"
    56  }
    57  
    58  //////////////////////////////////////////////
    59  
    60  // FromFloat32 converts value to a bool, 0 = false, else true
    61  func FromFloat32(v float32) bool {
    62  	if v == 0 {
    63  		return false
    64  	}
    65  	return true
    66  }
    67  
    68  // FromFloat64 converts value to a bool, 0 = false, else true
    69  func FromFloat64(v float64) bool {
    70  	if v == 0 {
    71  		return false
    72  	}
    73  	return true
    74  }
    75  
    76  // FromInt converts value to a bool, 0 = false, else true
    77  func FromInt(v int) bool {
    78  	if v == 0 {
    79  		return false
    80  	}
    81  	return true
    82  }
    83  
    84  // FromInt32 converts value to a bool, 0 = false, else true
    85  func FromInt32(v int32) bool {
    86  	if v == 0 {
    87  		return false
    88  	}
    89  	return true
    90  }
    91  
    92  // FromInt64 converts value to a bool, 0 = false, else true
    93  func FromInt64(v int64) bool {
    94  	if v == 0 {
    95  		return false
    96  	}
    97  	return true
    98  }
    99  
   100  // FromString converts string to a bool, "true" = true, else false
   101  func FromString(v string) bool {
   102  	if v == "true" {
   103  		return true
   104  	}
   105  	return false
   106  }