Solved

Method in java to remove decimal from string

Posted on 2009-03-30
6
1,022 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 

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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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
map related example 6 55
java jdbc batch example 8 41
Why my table column Id is not passed to java object? 4 44
ejb entity bean example 2 17
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

821 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