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....................

Recommended Answers

All 4 Replies

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.

public class CustomNumberType extends Number{

	public double doubleValue() {
		return 0;
	}

	public float floatValue() {
		return 0;
	}

	public int intValue() {
		return 0;
	}

	public long longValue() {
		return 0;
	}

}

Implement your logic in this class.

Regards,

Hello,

This is for you.

public class CustomNumberType extends Number{

	public double doubleValue() {
		return 0;
	}

	public float floatValue() {
		return 0;
	}

	public int intValue() {
		return 0;
	}

	public long longValue() {
		return 0;
	}

}

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..................

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:

if value is negative return "empty String"
else return the value

You need to explain your problem better and show some code

Then write a method that:

if value is negative return "empty String"
else return the value

You need to explain your problem better and show some code

Well I finally worked it out..........

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



import java.text.FieldPosition;
import java.text.NumberFormat;
import java.text.ParsePosition;

class MyNumberFormat extends NumberFormat {

    @Override
    public StringBuffer format(double number, StringBuffer toAppendTo,
            FieldPosition pos) {
        if(number>=0)
            return toAppendTo.append(number);
        else 
            return toAppendTo.append("");
    }

    @Override
    public StringBuffer format(long number, StringBuffer toAppendTo,
            FieldPosition pos) {
         if(number>=0)
            return toAppendTo.append(number);
        else
            return toAppendTo.append("");
    }

    @Override
    public Number parse(String source, ParsePosition parsePosition) {
        throw new UnsupportedOperationException();
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.