Two decimal places reserved for two integers of Java

Source: Internet
Author: User

in Java, when two integers are divided, because the number after the decimal point is truncated, the result of the operation will be an integer, and if you want to get the result of the operation as a floating point, you must cast both integers one or both to the floating-point numbers.
For example:
(float) A/b///convert one of the integers to a floating-point number and divide it with another integer
A /(float) b

(float) A/(float) b//convert two integers to floating-point numbers at the same time and then divide

float num= (float) 2/3;   DecimalFormat df = new DecimalFormat ("0.00");//Format fractional   string s = Df.format (num);//return String type





Two decimal places reserved for two integers of Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.