Solved

Method in java to remove decimal from string

Posted on 2009-03-30
6
1,019 Views
Last Modified: 2012-05-06
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
Comment
Question by:rheasam
  • 3
  • 2
6 Comments
 
LVL 9

Assisted Solution

by:brunoguimaraes
brunoguimaraes earned 100 total points
ID: 24021846
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
 

Author Comment

by:rheasam
ID: 24022043
Let me check
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 400 total points
ID: 24022288
All you need is the following, where 's' is the input string
s = "" + Integer.parseInt(s.replaceAll("\\.", ""));

Open in new window

0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:rheasam
ID: 24022514
CEHJ, will that take care of number like 05.05 also and make it 505

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24023329
Yes
0
 

Author Comment

by:rheasam
ID: 24023719
k, cool. I will award the points by tomorrow
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question