How to convert negative number to positive in Java?
来源:互联网 发布:淘宝旺铺基础版装修 编辑:程序博客网 时间:2024/06/05 17:03
To convert negative number to positive number (this is called absolute value), uses Math.abs(). This Math.abs() method is work like this “number = (number < 0 ? -number : number);".
Output:
See a complete example :
/** * 测试负数转正数 */@Testpublic void test2() {// TODO Auto-generated method stubint total = 1 + 1 + 1 + 1 + (-1);// output 3System.out.println("Total:" + total);int total2 = 1 + 1 + 1 + 1 + Math.abs(-1);// output 5System.out.println("Total 2 (absolute value):" + total2);}
Output:
Total : 3Total 2 (absolute value) : 5In this case, Math.abs(-1) will convert the negative number 1 to positive 1.
- How to convert negative number to positive in Java?
- How to determine negative number and positive number in Java?
- How to check if a Number is Positive or Negative in Java - Interview Question
- How to turn a negative outlook into a positive one
- How to convert char array to String in Java
- How to convert ArrayList to Set in Java with Example
- How to convert List<Integer> to int[] in Java?
- How to convert a Map to List in Java?
- Java : how to convert text to image
- Java: How to convert int[] to byte[]
- How to Convert gb2312 to utf8 in Perl!
- How to convert djvu to pdf in Ubuntu
- How to convert XML to JSON in ASP.NET C#
- How to convert std::string to LPCWSTR in C++ (Unicode)
- How to convert from int to string in objective c
- How to convert from int to string in objective c
- how to convert varchar to float in mysql
- How to convert XML to JSON in ASP.NET C#
- 数据库触发器----ORACLE
- How to determine negative number and positive number in Java?
- 设计模式--工厂模式
- Take Control Of Web Control ClientID Values in ASP.NET 4
- 哲理句摘录
- How to convert negative number to positive in Java?
- SQL的各种常用算法问题
- 理解inode
- jni例子
- linux安装jdk脚本
- 堆存储串的实现(c语言)
- 线性链表的表示以及实现(C语言编写)
- java 邮件
- MacOS X 安装 Mysql