Output the integer and decimal parts of the double type to two strings respectively.

Source: Internet
Author: User

// Test. cpp: defines the entry point for the console application.
//

# Include "stdafx. H"
# Include <stdio. h>
# Include <string. h>

Void formatdbltostr (double d_in, char * str_int, char * str_decmal );

Int main (INT argc, char * argv [])
{
Char str_int [8 + 1];
Char str_decmal [4 + 1];
 
Memset (str_int, 0x00, sizeof (str_int ));
Memset (str_decmal, 0x00, sizeof (str_decmal ));

Double D = 12.34;

Formatdbltostr (D, str_int, str_decmal );

Printf ("str_int = % s/n", str_int );
Printf ("str_decmal = % s/n", str_decmal );
Return 0;
}

Void formatdbltostr (double d_in, char * str_int, char * str_decmal)
{
/* Definition of local variables */
Char A [20];
Char B [20];
Char C [20];
Char E [20];

/* Initialize local variables */
Memset (A, 0x00, sizeof ());
Memset (B, 0x00, sizeof (B ));
Memset (C, 0x00, sizeof (c ));
Memset (E, 0x00, sizeof (e ));

/* Print the integer to */
Sprintf (a, "%. 0lf", d_in );

/* Print all to B */
Sprintf (B, "%. 4lf", d_in );

/* Copy the fractional part of B to C */
Memcpy (C, B + strlen (A) + 1, strlen (B)-strlen (a)-1 );

/* Print an integer of 8 digits */
Sprintf (E, "% 8.0lf", d_in );

/* Output result */
Memcpy (str_int, E, strlen (e ));
Memcpy (str_decmal, C, strlen (c ));
}

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.