[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1051
  • Last Modified:

Method in java to remove decimal from string

I am a newbie in java and need a method to remove any decimal from any length string and convert it without decimal. For example:

0.5555 should become 5555
0.045 should become 45
24.67 should become 2467
What is the simplest way to do this.
0
rheasam
Asked:
rheasam
  • 3
  • 2
2 Solutions
 
brunoguimaraesCommented:
See if this helps.
private static String removeDecimals(String number) {
		
		String numberNoDots = number.replace(".", "");
		int index = -1;
		
		for (int i = 0; i < numberNoDots.length(); i++)
			if (numberNoDots.charAt(i) != '0') {
				index = i;
				break;
			}
		
		return numberNoDots.substring(index);
	}

Open in new window

0
 
rheasamAuthor Commented:
Let me check
0
 
CEHJCommented:
All you need is the following, where 's' is the input string
s = "" + Integer.parseInt(s.replaceAll("\\.", ""));

Open in new window

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
rheasamAuthor Commented:
CEHJ, will that take care of number like 05.05 also and make it 505

0
 
CEHJCommented:
Yes
0
 
rheasamAuthor Commented:
k, cool. I will award the points by tomorrow
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now