Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Method in java to remove decimal from string

Posted on 2009-03-30
6
Medium Priority
?
1,045 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 9

Assisted Solution

by:brunoguimaraes
brunoguimaraes earned 400 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 1600 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.
Suggested Courses

650 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