How to display negative values in JSP
843835Jun 10 2002 — edited Jun 11 2002Hi All,
I want to display a negative value in jsp page. I have a variable of type double( called amount) which holds the price for some product. I was using the Method1 to display the amount value in JSP page. In this case, for negative value it used to display Amount in brackets like ($3647.98) if this amount is negative. ( amount can be negative in my case ). When I changed to Method2, it was able to display the -ve symbol without brackets but it was not in front of the amount rather at the TOP of amount.
example
List Price
- ( this - sign is just on TOP of 8 i.e. Aligned Right)
$3647.98
can somebody please tell me how should i change it to display like -$3647.98
Following is the code for both methods
Method1:
public String formatNumber( double parm ) {
NumberFormat pctfmt = NumberFormat.getCurrencyInstane( Locale.US );
return pctfmt.format( parm );
}
Method2:
public String formatNumber( double parm ) {
String newNumber = "";
double amount = 0.0;
NumberFormat pctfmt = NumberFormat.getCurrencyInstane( Locale.US );
try {
((DecimalFormat) pctfmt).toPattern();
amount = pctfmt.parse( pctfmt.format( parm )).doubleValue();
newNumber = Double.toString( amount );
} catch( ParseException e) {}
return newNumber.trim();
}
Thanks
Vinod