How to convert a positive decimal number to a decimal number of 2-9

Source: Internet
Author: User
Tags ibase

Convert any decimal places into 2, 3, 4, 5, 6, 7, 8, and 9 decimal places, retain 8 digits after the decimal point, and output. For example, if the decimal point is 0.795, the output is:

Decimal decimal number: 0.795000
Decimal decimal number: 0.795000
Decimal decimal number: 0.795000
Decimal decimal number: 0.795000
Decimal decimal number: 0.795000
Decimal decimal number: 0.795000
Decimal decimal number: 0.795000
Decimal decimal number: 0.795000
The following code provides this function. DTestNo indicates the decimal number to be converted. IBase indicates the number of hexadecimal digits.

Copy codeThe Code is as follows: # include <stdio. h>

Void fun (double dTestNo, int iBase)
{
Int iT [8]; // The converted decimal places are retained for 8 digits.
Int iNo;

Printf ("Convert decimal positive decimal % f to % d hexadecimal number:", dTestNo, iBase );

For (iNo = 0; iNo <8; iNo ++) // returns the decimal part of each digit.
{
DTestNo * = iBase;
IT [iNo] = (int) dTestNo; // obtain the integer part and store iT.
If (dTestNo> = 1) dTestNo-= iT [iNo]; // subtract the integer
}

Printf ("0 .");
For (iNo = 0; iNo <8; iNo ++) printf ("% d", iT [iNo]);
Printf ("\ n ");
}

Void main ()
{
Double dtestno= 0.795;
Int iBase;

For (iBase = 2; iBase <= 9; iBase ++)
Fun (dTestNo, iBase );
Printf ("\ n ");
}

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.