Custom NumberFormat

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Aug 2009
Posts: 63
Reputation: gunjannigam is an unknown quantity at this point 
Solved Threads: 8
gunjannigam gunjannigam is offline Offline
Junior Poster in Training

Custom NumberFormat

 
0
  #1
Aug 31st, 2009
I want to create a custom NumberFormat which returns empty string for negative values and the double number iteslf for +ve and zero values. Please guide me....................
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 121
Reputation: puneetkay is on a distinguished road 
Solved Threads: 23
puneetkay's Avatar
puneetkay puneetkay is offline Offline
Junior Poster

Re: Custom NumberFormat

 
0
  #2
Aug 31st, 2009
Originally Posted by gunjannigam View Post
I want to create a custom NumberFormat which returns empty string for negative values and the double number iteslf for +ve and zero values. Please guide me....................
Hello,

This is for you.
  1. public class CustomNumberType extends Number{
  2.  
  3. public double doubleValue() {
  4. return 0;
  5. }
  6.  
  7. public float floatValue() {
  8. return 0;
  9. }
  10.  
  11. public int intValue() {
  12. return 0;
  13. }
  14.  
  15. public long longValue() {
  16. return 0;
  17. }
  18.  
  19. }

Implement your logic in this class.

Regards,
Puneet Kalra
www.PuneetK.com
Sun Certified Java Programmer


Admin of Pikk - Object Relational Mapping Framework
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 63
Reputation: gunjannigam is an unknown quantity at this point 
Solved Threads: 8
gunjannigam gunjannigam is offline Offline
Junior Poster in Training

Re: Custom NumberFormat

 
0
  #3
Aug 31st, 2009
Originally Posted by puneetkay View Post
Hello,

This is for you.
  1. public class CustomNumberType extends Number{
  2.  
  3. public double doubleValue() {
  4. return 0;
  5. }
  6.  
  7. public float floatValue() {
  8. return 0;
  9. }
  10.  
  11. public int intValue() {
  12. return 0;
  13. }
  14.  
  15. public long longValue() {
  16. return 0;
  17. }
  18.  
  19. }

Implement your logic in this class.

Regards,
Sorry, I couldnt get you Puneet.


Wht I need in output is a object of class NumberFormat or ChoiceFormat which will have a label empty string for a -ve value and the number itself for +ve and zero values..................
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 229
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Custom NumberFormat

 
0
  #4
Aug 31st, 2009
Originally Posted by gunjannigam View Post
I want to create a custom NumberFormat which returns empty string for negative values and the double number iteslf for +ve and zero values. Please guide me....................
Then write a method that:
  1. if value is negative return "empty String"
  2. else return the value

You need to explain your problem better and show some code
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 63
Reputation: gunjannigam is an unknown quantity at this point 
Solved Threads: 8
gunjannigam gunjannigam is offline Offline
Junior Poster in Training

Re: Custom NumberFormat

 
0
  #5
Aug 31st, 2009
Originally Posted by javaAddict View Post
Then write a method that:
  1. if value is negative return "empty String"
  2. else return the value

You need to explain your problem better and show some code
Well I finally worked it out..........

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6.  
  7.  
  8. import java.text.FieldPosition;
  9. import java.text.NumberFormat;
  10. import java.text.ParsePosition;
  11.  
  12. class MyNumberFormat extends NumberFormat {
  13.  
  14. @Override
  15. public StringBuffer format(double number, StringBuffer toAppendTo,
  16. FieldPosition pos) {
  17. if(number>=0)
  18. return toAppendTo.append(number);
  19. else
  20. return toAppendTo.append("");
  21. }
  22.  
  23. @Override
  24. public StringBuffer format(long number, StringBuffer toAppendTo,
  25. FieldPosition pos) {
  26. if(number>=0)
  27. return toAppendTo.append(number);
  28. else
  29. return toAppendTo.append("");
  30. }
  31.  
  32. @Override
  33. public Number parse(String source, ParsePosition parsePosition) {
  34. throw new UnsupportedOperationException();
  35. }
  36. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 268 | Replies: 4
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC