• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

How to change format the following string?

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I want to format the string such that:
3.00 should return 3
3.20 should return 3.2
and 4.25 should return 4.25 (same)

Please note: I had a sub string version which I felt is not good. Hence I require an efficient way of coding this method.
Appreciate your help...!
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try this:
Double d = 3.56700;
String s = d.toString();
System.out.println(s);
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use System.out.printf or NumberFormat.
 
zahid zubair
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Thanks for your replies...
My point is, I already have a solution for this:

public class NumberConverter {

public static void main(String[] args) {

String someDynamicValue = "3.00";

if (someDynamicValue != null) {

int dynamicValueIndex = someDynamicValue.indexOf('.');
int dynamicValueLength = someDynamicValue.length();

if ( someDynamicValue.endsWith("00") ) {
someDynamicValue = someDynamicValue.substring(0, dynamicValueIndex);
} else if ( someDynamicValue.endsWith("0") ) {
someDynamicValue = someDynamicValue.substring(0, dynamicValueLength - 1 );
}
}

System.out.println("converted value is---->" + someDynamicValue);
}
}



but I am waiting for a better solution instead of making it much complex. Thanks for your help.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your better solution is using the DecimalFormat class. You can provide a patter which will only show non-zero digits, and there is a flag you can set to make sure the decimal point doesn't get shown in integer-cases.

Give it a try and if you get stuck let us know.
 
zahid zubair
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I got it using the DecimalFormat class:)

DecimalFormat d = new DecimalFormat("#.##");
d.setMaximumFractionDigits(2);
System.out.println(d.format(Double.parseDouble("3.245")));



Thanks yar. Thanks for your reply... It helps a lot.:)

However, I am seeing that the amount 3.245 is becoming 3.24 instead of 3.25. I believe there is definitely an api which can do this for me. But still I am searching on for it. ..
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

zahid zubair wrote:Hi,
I got it using the DecimalFormat class:)

DecimalFormat d = new DecimalFormat("#.##");
d.setMaximumFractionDigits(2);
System.out.println(d.format(Double.parseDouble("3.245")));



Thanks yar. Thanks for your reply... It helps a lot.:)

However, I am seeing that the amount 3.245 is becoming 3.24 instead of 3.25. I believe there is definitely an api which can do this for me. But still I am searching on for it. ..



Yup, The DecimalFormat object has a setRoundingMode() method. You will need to choose an appropriate RoundingMode
 
zahid zubair
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm. I got this setRoundingMode method. However, I am required to write this logic in Java 1.4 version, but I could find this method newly added only from version Java 1.6. Is there any other alternative for it?
 
Sheriff
Posts: 22682
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was thinking of using BigDecimal in combination with its stripTrailingZeros() method, which preserves 3.245 as 3.245. However, you've guessed it - the method has been created after Java 1.4.

That said, it shouldn't be too hard using simple String manipulation:
1) find the index of . - if it's not present, then you're done
2) from the end, check each character if it's 0. If it isn't go to step 3. Stop if you've reached the .
3) create a substring until the last non-0 character

You can possibly combine steps 1 and 2 into one for a single loop.
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

zahid zubair wrote:Hmmm. I got this setRoundingMode method. However, I am required to write this logic in Java 1.4 version, but I could find this method newly added only from version Java 1.6. Is there any other alternative for it?



That stinks. Why are you stuck with Java 1.4? It has been at End of Life for some time? Shouldn't you think about updating to a supported version of Java?

Anyway, in Java 1.4 DecimalFormat always rounded towards the even number for xxx5. So if it was 2.015 it would round to 2.02 and if it was 2.025 it would round to 2.02 which I think is used because it more evenly disperses rounded values. the only way I know how to make sure it always makes half-values round up would be to use BigDecimal and set the scale.

Something like:
 
zahid zubair
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I tried the above suggestion. I think this works fine. Thanks for all your help...


Regards,
Zahid Zubair
 
What's gotten into you? Could it be this tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic