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

  • Home
  • SEARCH
  • 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 188231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T15:58:12+00:00 2026-05-11T15:58:12+00:00

How can I format a number to a fixed number of decimal places (keep

  • 0

How can I format a number to a fixed number of decimal places (keep trailing zeroes) where the number of places is specified by a variable?

e.g.

int x = 3; Console.WriteLine(Math.Round(1.2345M, x)); // 1.234 (good) Console.WriteLine(Math.Round(1M, x));      // 1   (would like 1.000) Console.WriteLine(Math.Round(1.2M, x));    // 1.2 (would like 1.200) 

Note that since I want to control the number of places programatically, this string.Format won’t work (surely I ought not generate the format string):

Console.WriteLine(     string.Format('{0:0.000}', 1.2M));    // 1.200 (good) 

Should I just include Microsoft.VisualBasic and use FormatNumber?

I’m hopefully missing something obvious here.

  • 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. 2026-05-11T15:58:13+00:00Added an answer on May 11, 2026 at 3:58 pm

    Try

    decimal x = 32.0040M; string value = x.ToString('N' + 3 /* decimal places */); // 32.004 string value = x.ToString('N' + 2 /* decimal places */); // 32.00 // etc. 

    Hope this works for you. See

    http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx

    for more information. If you find the appending a little hacky try:

    public static string ToRoundedString(this decimal d, int decimalPlaces) {     return d.ToString('N' + decimalPlaces); } 

    Then you can just call

    decimal x = 32.0123M; string value = x.ToRoundedString(3);  // 32.012; 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: PHP: show a number to 2 decimal places How can I format
I want to know how can i format a number like 2000000 into standard
How can I convert an Excel date (in a number format) to a proper
In rml reports how can i change the current page number format from page:1
Can anyone tell me how to format a given number of seconds (for example
first time SO user :) I know that I can format a number like
With NSNumberFormatter it is very easy to format a number using a fixed string
I'm trying to create a function that can format a number with the minimum
Does any one know how can I format the number and limited it to
The Date format can be customized in Oracle SQL Developer, how Decimal format can

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.