Truncate (shorten) a floating point value at given precision
(number, precision, round=False)
| 2156 | |
| 2157 | |
| 2158 | def truncate_float(number, precision, round=False): |
| 2159 | """Truncate (shorten) a floating point value at given precision""" |
| 2160 | |
| 2161 | # don't allow a negative precision [by mistake?] |
| 2162 | precision = abs(precision) |
| 2163 | |
| 2164 | if round: |
| 2165 | # python 2.7+ supported method [recommended] |
| 2166 | short_float = round(number, precision) |
| 2167 | |
| 2168 | # python 2.6+ supported method |
| 2169 | """short_float = float("{0:.{1}f}".format(number, precision)) |
| 2170 | """ |
| 2171 | |
| 2172 | else: |
| 2173 | operate_on = 1 # returns the absolute number (e.g. 11.0 from 11.456) |
| 2174 | |
| 2175 | for _ in range(precision): |
| 2176 | operate_on *= 10 |
| 2177 | |
| 2178 | short_float = float(int(number * operate_on)) / operate_on |
| 2179 | |
| 2180 | return short_float |
| 2181 | |
| 2182 | |
| 2183 | def get_time_until_next_month(): |
no outgoing calls
no test coverage detected