Answers the formatted string of value. Use the format string if defined. Otherwise answer the cleanest representation, eating all 0 and /. from the right side. >>> asFormatted(100) '100' >>> asFormatted(100.00) '100' >>> asFormatted(100.100000) # Eats trailing zero, unti
(value, default=None, hasFormat=None)
| 173 | return value |
| 174 | |
| 175 | def asFormatted(value, default=None, hasFormat=None): |
| 176 | """Answers the formatted string of value. Use the format string if defined. |
| 177 | Otherwise answer the cleanest representation, eating all 0 and /. from the |
| 178 | right side. |
| 179 | |
| 180 | >>> asFormatted(100) |
| 181 | '100' |
| 182 | >>> asFormatted(100.00) |
| 183 | '100' |
| 184 | >>> asFormatted(100.100000) # Eats trailing zero, until non-zero decimal |
| 185 | '100.1' |
| 186 | >>> asFormatted(100.12789) # Round to 2 digits |
| 187 | '100.13' |
| 188 | >>> asFormatted(100.99) # Round to 2 digits, then eats zeros |
| 189 | '100.99' |
| 190 | >>> asFormatted(100.999) # Round to 2 digits, then eats zeros |
| 191 | '101' |
| 192 | >>> asFormatted(100.100002345) # Round to 2 digits, then eats zeros |
| 193 | '100.1' |
| 194 | >>> asFormatted(100.000001) # Eats the decimal point, not the integer zeros |
| 195 | '100' |
| 196 | >>> asFormatted(None, 100.00) # Use formatted default |
| 197 | '100' |
| 198 | >>> asFormatted(200/3) # Default rounds to 2 digits. |
| 199 | '66.67' |
| 200 | >>> asFormatted(200/3, hasFormat='%0.10f') # Overwrite behavior by supplied format string |
| 201 | '66.6666666667' |
| 202 | """ |
| 203 | if value is None: |
| 204 | value = default |
| 205 | if hasFormat is None: |
| 206 | iNumber = asNumber(value) |
| 207 | |
| 208 | if isinstance(iNumber, int): # Check on rounded by 0.00 |
| 209 | return '%d' % iNumber |
| 210 | value = '%0.2f' % value # Round to 2 digits |
| 211 | |
| 212 | # Then remove any trailing zeros (in case of a decimal point) |
| 213 | while value and '.' in value and value.endswith('0'): |
| 214 | value = value[:-1] |
| 215 | if value and value.endswith('.'): |
| 216 | value = value[:-1] # Eat remaining period on the right. |
| 217 | |
| 218 | return value or '0' # Answer value. If all eaten, then just answer 0 |
| 219 | |
| 220 | return hasFormat % value # Otherwise show as float with 2 digits. |
| 221 | |
| 222 | def value2Tuple4(v): |
| 223 | """Answers a tuple of 4 values. Can be used for colors and rectangles. |
no test coverage detected