Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7751221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:29:31+00:00 2026-06-01T11:29:31+00:00

You can truncate strings with a printf field-width specifier: printf(%.5s, abcdefgh); > abcde Unfortunately

  • 0

You can truncate strings with a printf field-width specifier:

printf("%.5s", "abcdefgh");

> abcde

Unfortunately it does not work for numbers (replacing d with x is the same):

printf("%2d",   1234);  // for 34
printf("%.2d",  1234);  // for 34
printf("%-2d",  1234);  // for 12
printf("%-.2d", 1234);  // for 12

> 1234

Is there an easy/trivial way to specify the number of digits to be printed even if it means truncating a number?

MSDN specifically says that it will not happen which seems unnecessarily limiting. (Yes, it can be done by creating strings and such, but I’m hoping for a “printf trick” or clever kludge.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T11:29:33+00:00Added an answer on June 1, 2026 at 11:29 am

    Like many of my best ideas, the answer came to me while lying in bed, waiting to fall asleep (there’s not much else to do at that time than think).

    Use modulus!

    printf("%2d\n", 1234%10);   // for 4
    printf("%2d\n", 1234%100);  // for 34
    
    printf("%2x\n", 1234%16);   // for 2
    printf("%2x\n", 1234%256);  // for d2
    

    It’s not ideal because it can’t truncate from the left (e.g., 12 instead of 34), but it works for the main use-cases. For example:

    // print a decimal ruler
    for (int i=0; i<36; i++)
      printf("%d", i%10);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Quick disclaimer: I saw some plugins that truncate text strings but they do not
How can the fixed column width could be respected even when the strings are
I have a string headerText, which containing text ABC (2-8 Days). How can truncate
Using MSSQL2005, can I truncate a table with a foreign key constraint if I
can someone help me with unix command to truncate the contents of the files
How can I truncate a string after 20 words in PHP?
I'm having an issue where some strings are overflowing their container. I can, of
How can I truncate string in JSF2 ? Say I have a really long
How can i truncate a text to the closest position with rails 3 whithout
Can somebody point me to a resource that explains how to go about having

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.