I am using a library that lets me configure the way numbers are formatted using a DecimalFormat pattern. I need to remove the minus symbol to show the absolute value of the numbers. I have tried both "0.00###;0.00###" and "0.00###;#" without success. I can choose any minus symbol (eg "0.00###;(0.00###)") but I can't have no sign at all?
Thanks in advance for your suggestions,
Tom
why can't you call Math.abs() before formatting your number?
int myNum = -123;
myNum = Math.abs(myNum);
System.out.println(myNum); // 123
If you really can't use absolute values with Math.abs
as mentionned in other answers, you could change the minus sign in the DecimalFormatSymbols of your DecimalFormat
. Beware that you need to set back the value into your DecimalFormat
since it returns a different instance when calling getDecimalFormatSymbols .
You could also use DecimalFormat.setNegativePrefix("") as kdgregory commented.
如果使用“0.00 ###; 0.00 ###”(注意分号后面的空格),则不会显示负号。
它可能不是你想要的,但为什么不使用Math.abs()并简单地做:
new DecimalFormat("0.00###").format(Math.abs(value))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.