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

x
Solved

# formatting a string in java

Posted on 2008-06-09
Medium Priority
258 Views
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
Question by:mithunda5011
[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
• 2
• 2

LVL 9

Accepted Solution

mbodewes earned 2000 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);
``````
0

LVL 9

Expert Comment

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

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

Author Closing Comment

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

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…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
###### Suggested Courses
Course of the Month9 days, 10 hours left to enroll