Displays the integer and fractional parts of a floating-point number separately, in Java display (1H)

Source: Internet
Author: User

Displays the integer and fractional parts of a floating-point number separately, and the Java display
For example:
Please enter any decimal: 16.69
Integer part: 16
Fractional part: 0.69

The code implementation is as follows:


 * * Please enter any decimal: 16.69
     integer part:
     decimal part: 0.69
 * * */Public
class Dividefloatnumber {public
    static void Main (string[] args) {
        float inputfloat=0f;
        String floatstring;
        int pointindex = 0;//decimal Position
        //Enter a floating-point number
        Scanner Scanner = new Scanner (system.in);
        System.out.println ("Please enter a floating-point number:");
        Inputfloat = Scanner.nextfloat ();

        The integer portion of the output floating-point number
        System.out.println (inputfloat+ "" + (int) inputfloat);

        Output floating-point number of decimal parts, note the positive sign
        floatstring = inputfloat+ "";
        PointIndex = Floatstring.indexof ('. ');
        Integral part of System.out.println (inputfloat+ ":
                +float.parsefloat (floatstring.charat (0) = = '?" -":" ")
                    +" 0. " +floatstring.substring ((pointindex+1), Floatstring.length ())));
    }

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.