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 8612121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:32:56+00:00 2026-06-12T04:32:56+00:00

How can I use String.Format in C# so doubles are displayed like this: Values:

  • 0

How can I use String.Format in C# so doubles are displayed like this:

Values:

-1.0
1.011
100.155
1000.25
11000.52221

displayed string:

-1.00
1.011
100.2
 1000
11001

The main point is my width is fixed to 5 characters no matter what. I don’t really care how many decimal places are shown to the right. If there are 4 or more numbers to the left of decimal I want everything right of the decimal to be dropped (including the decimal itself).

It seems like something that should be a pretty standard practice. But I’m not having much luck finding an answer that works.

A couple of corrections were made to the display string above, I do want rounding.

Thanks!

  • 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-12T04:32:57+00:00Added an answer on June 12, 2026 at 4:32 am
    public string FormatNumber(double number)
    {
        string stringRepresentation = number.ToString();
    
        if (stringRepresentation.Length > 5)
            stringRepresentation = stringRepresentation.Substring(0, 5);
    
        if (stringRepresentation.Length == 5 && stringRepresentation.EndsWith("."))
            stringRepresentation = stringRepresentation.Substring(0, 4);
    
        return stringRepresentation.PadLeft(5);
    }
    

    EDIT: Just realized that this doesn’t pad zeros at the end of the decimal if necessary (as in your first example) but should give you the tools to finish it off as you need to.

    EDITx2: Given your most recent addition that you intend to have rounding, it gets more complicated. First you have to do a check to see if you will have any decimal places and at what position the decimal is. Then you have to round it to that decimal place, then probably run through the output. Note that depending on your algorithm, you could get some incorrect results where rounding rolls over numbers (for example, -10.9999 could become -11.00 or -11 depending on your implementation)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I use String.Format in C# so doubles are displayed like this: example:
Just curious, is there a format string I can use to output something like
I have a number as a string like this: 9.756088256835938E-4 but I only can
If I use String.intern() to improve performance as I can use == to compare
The max number of characters you can use in string in a vba function
I am getting the below perl error. Can't use string () as a symbol
How can I use emacs string-insert-rectangle operation to add a vector of numbers to
Possible Duplicate: How can I use a string with the same name of an
I can use the following codes to trim a string: -(void) aMethod { //
I need to parse a XML String so I can use some of the

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.