Solved

Method in java to remove decimal from string

Posted on 2009-03-30
6
1,027 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 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jboss 7.1 start up error 1 80
ejb entity bean example 2 52
Html Table Looping (part 2) 5 39
junit as external jar or library 7 54
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

740 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