1

I have the following class

public class Foo {
   private long var1;
   private float var2;
   private double var3;
   /* getter and setter

    */
}

To print a numeric value to command line, I use String.valueOf(obj.getVar2()), but this will result in 9.888888888. Are there any ways, to produce formatted outputs for all numeric getters (float, double)? I have a function 'bar' , which will format a given numeric value to a string. Now, I want to call functions like obj.getVar2().toString(), to get the numeric value back as a formatted string from 'bar'. Is it possible?

user3417078
  • 265
  • 4
  • 15
  • 4
    "*Now, I want to call functions like `obj.getVar2().toString()`, to get the numeric value back as a formatted string from `bar`. Is it possible?*" do you get any errors when you try it? – Pshemo Jul 08 '15 at 16:49
  • 2
    Yes, there are ways and it is possible. – tnw Jul 08 '15 at 16:50
  • Refer [format-float-to-n-decimal-places](http://stackoverflow.com/questions/5195837/format-float-to-n-decimal-places) – Madhan Jul 08 '15 at 16:50
  • I think you should initiate a float/double OBJECT instead of the primitive ones in your getter(), from there you can do obj.getVar2().toString(). – Ramin Omrani Jul 08 '15 at 16:52
  • @ MadhanThat the function. Thanks ;) @ Ramin Omrani Possible, but Long.toString() doesn't support my formatted string. – user3417078 Jul 08 '15 at 17:14

3 Answers3

1

You can add methods to your Foo class:

public String getVar1AsString() {
    return Utils.bar(var1);
}

But you can not change the semantics of Float.toString() or Double.toString() to use bar internally.

An alternative would be to provide your own CustomFloat or CustomDouble classes and use them everywhere in your app.

BladeCoder
  • 12,779
  • 3
  • 59
  • 51
  • I had this idea too, but I don't want to have X string-getters for x numeric values. Okay, I could place the formatted function inside my class and call toFormattedString(getVar1()).... I would prefer a simple prefix (like toString()), to get a string value. – user3417078 Jul 08 '15 at 17:18
  • 1
    I understand, but Java does not support "extension methods", also called "categories" in objective C. – BladeCoder Jul 08 '15 at 18:11
  • Thank you, that's I want to know. – user3417078 Jul 08 '15 at 19:11
1

There are many ways to get formatted numbers:

class Foo {
    private long var1;
    private float var2;
    private double var3;

    public double getVar3() { return var3; }
    public float getVar2() { return var2; }
    public long getVar1() { return var1; }

    public String getFormatVar3() {
        // third way
        return String.format("%XXX", var3);
        // or
        return this.bar(var3);
    }
    @Override
    public String toString() {
        // first way
        return String.format("%XXX %XXX %XXX", var1, var2, var3);
    }

    public static void main(String[] args) {
        // second way
        Foo foo = new Foo();
        String format = String.format("%XXX", foo.getVar1());
    }

    private String bar(Number n) {
        // discover type of number
        return String.format("%XXX", n);
    }
}

where, %XXX is your format.

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
0

If bar is non-static method - obj.bar(obj.getVar2()); If bar is static method - Foo.bar(obj.getVar2());

By the way your class definition is wrong. It should be public class Foo { not public class Foo() {

Raman Shrivastava
  • 2,923
  • 15
  • 26
  • This was my first solution. As BladeCoder already said, 'categories' aren't available in Java, so your solution works best in my case. – user3417078 Jul 08 '15 at 19:13