shift_decimal_to_int

pyhelpers.ops.shift_decimal_to_int(value, precision=20)[source]

Converts a float value to an integer by simply removing its decimal point.

The function shifts the decimal point to the right until it disappears, effectively multiplying by the appropriate power of 10 to eliminate the fractional part. For example, 12.345 becomes 12345 (×1000).

Parameters:
  • value (float | int) – The float value to convert. Can be positive, negative, or zero.

  • precision (int) – The maximum number of digits after the decimal point. Defaults to 20.

Returns:

Integer with the decimal point removed.

Return type:

int

Raises:

TypeError – If value is not a number.

Examples:

>>> from pyhelpers.ops import shift_decimal_to_int
>>> shift_decimal_to_int(12.345)
12345
>>> shift_decimal_to_int(0.056)
56
>>> shift_decimal_to_int(-3.14)
-314
>>> shift_decimal_to_int(1000.0)
10000
>>> shift_decimal_to_int(1.2300)
123
>>> shift_decimal_to_int(0.0005)
5
>>> shift_decimal_to_int(1.2e-15)
12

Note

  • Trailing zeros are preserved (1.2300 → 12300)

  • The conversion uses string manipulation for accuracy and speed