Solved

formatting a string in java

Posted on 2008-06-09
4
252 Views
Last Modified: 2013-11-23
hello,
i hv a string like this "<00001.1296". i want to format it like this--"<1.13".there will b 2 places after decimal point(rounded up) and only non zero digits before decimal point.can nebody pls help
thnx
0
Comment
Question by:mithunda5011
  • 2
  • 2
4 Comments
 
LVL 9

Accepted Solution

by:
mbodewes earned 500 total points
ID: 21744881
First parse the double contained in the String. So you would have to skip the first "<" character. Now you have to round, but how: Math.round() only rounds to long. Ok, first get it right by multiplying with (10 ^ digits behind). Then round, and divide again with the (10 ^ digits behind). Now you will just have to print the thing.

Of course it will still print an initial 0 if the value itself is smaller than 1. You can check for the first character of the printed out string and remove the 0 from the front (using substring again) if you want to remove that leading zero as well (so you could get .01 instead of 0.01...).

String doubleDo = "<00001.1296";
String theDoubleStr = doubleDo.substring(1);
double d = Double.parseDouble(theDoubleStr);
double pow = Math.pow(10, 2);
 
long value = Math.round(d * pow);
double roundedValue = (double) (value / pow);
System.out.printf("<%.2f", roundedValue);

Open in new window

0
 
LVL 9

Expert Comment

by:mbodewes
ID: 21744934
Note that the part after the substring should be in a separate method, and that the variables have been named in a horrible fashion, I'm sure you can do better :)
0
 

Author Comment

by:mithunda5011
ID: 21744999
this was fantastic fantastic fantastic but sorry i cant give you more than 500 frendddddddd
0
 

Author Closing Comment

by:mithunda5011
ID: 31465451
i m gr8ly impressed...many times i face this type of logical problem...do you offer professional service ? i m from india and my id is debnath.chakraborty@gmail.com...i face 20-30 this kind of problem per month ...sometimes also related to database...i can provide 5 dollers per solution can u help? pls never mind if i may seem too bold....gr8 work
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
javap not working 8 59
Why my table column Id is not passed to java object? 4 44
Android development question 2 54
Html Table Looping (part 2) 5 27
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

828 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