Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,844 questions

55,671 answers

573 users

How to remove trailing zeros from a string in Java

5 Answers

0 votes
public class Main {
    public static String removeTrailingZeros(String str) {
        // Find the position of the last non-zero character
        int end = str.toString().lastIndexOf('0');
        while (end > 0 && str.charAt(end) == '0') {
            end--;
        }

        // Erase all characters after the last non-zero character
        if (end != -1) {
            str = str.substring(0, end + 1);
        } else {
            // If the string is all zeros, erase the entire string
            str = "";
        }

        return str.toString();
    }

    public static void main(String[] args) {
        String str = "457833.8591000";

        str = removeTrailingZeros(str);

        System.out.println(str);
    }
}



/*
run:

457833.8591

*/

 



answered Nov 15, 2024 by avibootz
0 votes
public class Main {
    public static String removeTrailingZeros(String str) {
        StringBuilder sb = new StringBuilder(str);
        
        while (sb.length() > 0 && sb.charAt(sb.length() - 1) == '0') {
            sb.setLength(sb.length() - 1);
        }
        
        return sb.toString();
    }

    public static void main(String[] args) {
        String str = "457833.8591000";

        str = removeTrailingZeros(str);

        System.out.println(str);
    }
}



/*
run:

457833.8591

*/

 



answered Nov 15, 2024 by avibootz
0 votes
public class Main {
    public static String removeTrailingZeros(String str) {
        int index;
        for (index = str.length() - 1; index >= 0; index--) {
            if (str.charAt(index) != '0') {
                break;
            }
        }
        
        return str.substring(0, index + 1);
    }

    public static void main(String[] args) {
        String str = "457833.8591000";

        str = removeTrailingZeros(str);

        System.out.println(str);
    }
}



/*
run:

457833.8591

*/

 



answered Nov 15, 2024 by avibootz
0 votes
import java.math.BigDecimal;

public class Main {
    public static String removeTrailingZeros(String str) {
        return new BigDecimal(str).stripTrailingZeros().toPlainString();
    }

    public static void main(String[] args) {
        String str = "457833.8591000";

        str = removeTrailingZeros(str);

        System.out.println(str);
    }
}



/*
run:

457833.8591

*/

 



answered Nov 15, 2024 by avibootz
0 votes
class Main {
    public static void main(String[] args) {
        String str = "457833.8591000";
        
        str = removeTrailingZeros(str);

        System.out.println(str);
    }

    public static String removeTrailingZeros(String str) {
        str = str.replaceAll("0*$", ""); // Remove trailing zeros
        str = str.replaceAll("\\.$", ""); // If there's a dot before zeros (.000), remove it too
        
        return str;
    }
}



/*
run:

457833.8591

*/

 



answered Nov 15, 2024 by avibootz

Related questions

1 answer 178 views
1 answer 182 views
1 answer 186 views
1 answer 158 views
1 answer 169 views
1 answer 162 views
...