The realization method of converting positive decimals into 2-9 decimal digits _c language

Source: Internet
Author: User
Tags ibase

Converts any decimal positive decimal to a 2,3,4,5,6,7,8,9 decimal, which retains 8 digits after the decimal point, and outputs. For example: If the decimal decimal is 0.795, the output is:

Decimal positive decimal 0.795000 converted to 2 in number: 0.11001011
Decimal positive decimal 0.795000 converted to 3 in number: 0.21011011
Decimal positive decimal 0.795000 converted to 4 in number: 0.30232011
Decimal positive decimal 0.795000 converted to 5 in number: 0.34414141
Decimal positive decimal 0.795000 converted to 6 in number: 0.44341530
Decimal positive decimal 0.795000 converted to 7 in number: 0.53645364
Decimal positive decimal 0.795000 converted to 8 in number: 0.62702436
Decimal positive decimal 0.795000 converted to 9 in number: 0.71348853
The following code provides this functionality. Where Dtestno represents the decimal decimal number to be turned on. IBase represents the number of numbers in the system.

Copy Code code as follows:

#include <stdio.h>

void Fun (double dtestno, int ibase)
{
int it[8]; The converted decimal number retains 8 digits
int iNo;

printf ("Decimal positive decimal%f converted to%d numbers:", Dtestno, IBase);

for (ino=0;ino<8;ino++)///per digit decimal parts respectively
{
Dtestno *= IBase;
It[ino] = (int) Dtestno; To get the integer part stored up
if (dtestno>=1) Dtestno-= It[ino]; Minus the whole number of parts
}

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.