How to strip integers of trailing zeros

python remove leading zeros from list
python remove trailing zeros from float
python strip leading numbers
python trim leading and trailing zeros
pyspark remove leading zeros
how to remove zeros after decimal in python
python trailing zeros
how to remove leading zeros in java

Given a set of integers (e.g. {1000000, 20000000, 1234000, 1200000}), I want to apply to all of them a function such that:

  1. the magnitude of a number is lowered as far as possible
  2. all numbers remain integers
  3. their relative proportions remain constant

In other words, I want to strip as many zeros as possible without losing any information other than absolute magnitude, so the set would become {1000, 20000, 1234, 1200}

Is there a term for this operation and is there an efficient Python function, or should I just quickly code this?

Edit: This solution is not a duplicate because it deals with singular numbers - in my case, the number of zeros depends on the particular set.

Edit 2: Green Cloak Guy provided a solution for my exact requirements, and Illmora one that does what I should have actually conceptualized in the first place.

Given all you care about here is decreasing magnitude, have you considered just representing your numbers as Decimals and then printing them in scientific notation?

from decimal import Decimal

nums = {Decimal(1000000), Decimal(20000000), Decimal(1234000), Decimal(1200000)}
print({str(num.normalize()) for num in nums})
# {'1E+6', '1.2E+6', '2E+7', '1.234E+6'}

If that's not reasonable for your use case, then another thing you can do is essentially determine the maximum magnitude you can reduce by, and then reduce by that much. For magnitudes of 10, this is fairly simple and you can use strings to do it:

nums = {1000000, 20000000, 1234000, 1200000}
div_factor = 10 ** min(len(str(num)) - len(str(num).rstrip('0')) for num in nums)
reduced_nums = {num / div_factor for num in nums}
# {1000.0, 1234.0, 20000.0, 1200.0}
# you can use integer division `//` instead of true division `/` if you want

For nonstandard magnitudes (e.g. magnitudes of 3), you'd need to get more creative and come up with a way to efficiently figure out the largest magnitude you can divide by. My example above takes a shortcut here by checking how many digits disappear when we cut out the trailing zeroes (which is equivalent to checking the largest exponent of 10 that can be integer-divided into the number). Since python doesn't have a built-in way to print in bases that aren't 2, 8, 10, or 16, you'll have to figure out your own solution.

Remove leading and trailing zeroes, a=>{ f=a=>a.reverse().filter(x=>a|=x) // reverse and remove leading 0 // leverage js cast rules: operator | cast operands to integer // an array casted to integer is 0  A simple method is to first calculate factorial of n, then count trailing 0s in the result (We can count trailing 0s by repeatedly dividing the factorial by 10 till the remainder is 0). The above method can cause overflow for a slightly bigger numbers as factorial of a number is a big number (See factorial of 20 given in above examples).

Looking at your requirements, what you are after can be easily done by dividing each input number by the GCD (Greatest Common Denominator) of all the input numbers.

#!/usr/bin/env python3

import math
from functools import reduce

numbers = [1000000, 20000000, 1234000, 1200000]

# Find the greatest common denominator
gcd = reduce(lambda x,y: math.gcd(x,y), numbers)

# Divide each number by the GCD
minimum_numbers = map(lambda x: int(x/gcd), numbers)

print(*minimum_numbers, sep=',')

With your input numbers, it produces this result:


Because of the properties of the GCD, the output is guaranteed to be the lowest possible integer that still maintains the relative proportions between each number.

Remove Trailing Zeros From string in C++, Given a string of digits, remove trailing zeros from it. Examples: string removeZero(string str). {. // Count trailing zeros. int i = 0;. while (str[i] == '0' ). i++;. This is my fist post, so please excuse any lack of etiquette. My question is how to remove the trailing zeros on a string. The following is my fictional dataset and below that is how I wish it would look after a datastep. Ignore the decimal point part of the code, since I need that for a subsequent step not related to dropping the trailing zeros.

Don't kwow if there's a more efficient way. I would use:

import numpy as np

def reduce(array):

    mult = [0.1]*len(array)

    while all(item%10 == 0 for item in array):
        array = np.multiply(array, mult)

    return array


intgrs = (1000000, 20000000, 1234000, 1200000)

It will return a numpy array with the following values: [1000 20000 1234 1200]

Remove Leading Zeros From String in Java, Name of the class to remove leading/preceding zeros */. class RemoveZero. {. public static String removeZero(String str). {. // Count leading zeros. int i = 0 ;. Excel automatically removes leading zeros, as well as trailing zeros after a decimal point. If this disrupts your ability to store data such as postal codes, you can import the data as text so it appears exactly as entered. If you need to use the data in calculations, set it to display in a custom format instead.

Not so gentle, but it works.

def trim_zeros(nums):
  while 1:
    for i in nums:
        if str(i)[-1] != "0":
    nums=[int(i/10) for i in nums]

How to remove leading and trailing zeros in a string in Python, Removing leading and trailing zeros deletes them from a string. For example, removing the leading and trailing zeros from "00abc00" results in "abc" . To pad an integer number with leading and trailing spaces/zeroes, we can use String.Format() method which is library method of String class in C#. using System; namespace ConsoleApplication1 {class Program {static void Main (string [] args) {Console. WriteLine (" Demo for left or right alignment of an integer number: "); Console. WriteLine (String.

Just in case you don't worry about the ordering of the set elements.

 sets = {1000000, 20000000, 1234000, 1200000}
 max = max([len("".join(map(str, str(i))).rstrip('0')) for i in sets])
 new_set = {int(str(i)[:max]) for i in sets}  # gives {1000, 1234, 1200, 2000}

php remove trailing zeros of a decimal number – web-profile, Method to remove or strip trailing zeros of a decimal number: if($pos === false) { // it is integer number return $num; }else{ // it is decimal number return  Remove trailing zeros from numbers 23560000,10000,456 78650000,560000,54 34000000,3456,3 The first column has 4 trailing zeros. I have to remove 4 trailing zeroes

Tableau Tip: All about Leading and Trailing Zeros, Handling numbers as text for the first time in Tableau is a frustrating experience. Depending on your Removing Trailing Zeros. This scenario  I suppose this can take care of the trailing decimal point for integers: SELECT REPLACE (REPLACE (RTRIM (REPLACE (@a, '0',' ')), ' ','0') + ' ', '.

Remove trailing zeros - FAQ - Dynamo, Everyone, Please show me the way to remove trailing zeros in number shown in the below List of numbers from Excel - "String from Object" adds decimals. Let’s say the following is our string with leading zeros. String str ="000234"; Use the TrimStart () method and set the 0 to remove it. TrimStart (new Char [] { '0' })

FORMAT, This script function can be used to add or remove leading zeros to or from a number. In floating-point numbers, you can also specify the number of positions that  Interactive math practice for 4000+ skills. Proven success. Sign up today!