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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:04:17+00:00 2026-05-29T22:04:17+00:00

Is there a way to format a double number that always have n digits

  • 0

Is there a way to format a double number that always have n digits sepecified by user?

For example if user want to see always 4 digits, take the following numbers as example:

Original             Formatted
-------             ---------
3.42421             3.424
265.6250            265.6
812.50              812.5
12.68798            12.68
0.68787             0.687

I made up this but it just allows for number of floating points! it is not what I wanted!

    public string ToEngV(double d, int percision = 0)
    {
        string zeros = string.Empty;

        if (percision <= 0)
        {
                zeros += "0";
        }
        else if (percision > 0)
        {
            for (int i = 0; i < percision; i++)
            {
                zeros += "0";
            }
        }

       return String.Format("{0:0." + zeros + "}", d) 
     }

Imagine I call the above method for a number like 812.50 and I set the precision to (this is now used for all numbers I am going to format). Obviously the output will be 812.5

But if I give the another number like 1.61826 I will get 1.6 and this ruins the formatting in the page I show these number to users. I need that to be 1.618

Thus I want my method to always show N digit!

  • 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-05-29T22:04:19+00:00Added an answer on May 29, 2026 at 10:04 pm

    I’m not sure if your asking to round or truncate numbers, so I wrote this method:

    public static string ToEngV(this double d, int digits, bool round)
    {
        var lenght = Math.Truncate(d).ToString().Length;
    
        if (lenght > digits)
        {
            throw new ArgumentException("...");
        }
    
        int decimals = digits - lenght;
    
        if (round)
        {
            return Math.Round(d, decimals).ToString();
        }
        else
        {
            int pow = (int)Math.Pow(10, decimals);
            return (Math.Truncate(d * pow) / pow).ToString();
        }
    }
    

    Example:

    var numbers = new double[] { 3.42421, 265.6250, 812.50, 12.68798, 0.68787 };
    foreach (var number in numbers)
    {
        Console.WriteLine(number.ToEngV(4, false));
    }
    Console.WriteLine()
    foreach (var number in numbers)
    {
        Console.WriteLine(number.ToEngV(4, true));
    }
    

    Output:

    3.424
    265.6
    812.5
    12.68
    0.687
    
    3.424
    265.6
    812.5
    12.69
    0.688
    

    Note that if your number has more integer digits than digits you will get an ArgumentException.

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

Sidebar

Related Questions

Is there a way to format in PHP a number always keeping its sign
Is there a simple way to format that single heading and turn those heading
Let's say I have a number (e.g. double) and transform it using some format
Is there any way to format a string by name rather than position in
Is there a way to format a UTC time into any arbitrary string format
Is there a way to format a decimal as following: 100 -> 100 100.1
Is there any way to format this so it's a valid expression, without adding
Is there any way to format a USB mass storage device programmatically in Android?
Is there a way to format the output of Select-String to include newlines between
Is there any way to down-format a Subversion repository to avoid messages like this:

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.