473,385 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Number Rounding/Formating

Hi,

In my application I need to round a number to 3 decimal places. I am using
the Math.Round method which works fine. The problem I am running into is
when the number has no decimals like 5 because I need to show it as 5.000.
How can I accomplish this?

Thanks
Nov 15 '05 #1
4 1471
<"Eric" <nospam>> wrote:
In my application I need to round a number to 3 decimal places. I am using
the Math.Round method which works fine. The problem I am running into is
when the number has no decimals like 5 because I need to show it as 5.000.
How can I accomplish this?


Use a custom format string, e.g.

Console.WriteLine ("{0:0.000}", 5.5);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
I do not want to use WriteLine. I want to do it inside my business objects.
Is there another way?

Thanks

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m...
<"Eric" <nospam>> wrote:
In my application I need to round a number to 3 decimal places. I am using the Math.Round method which works fine. The problem I am running into is when the number has no decimals like 5 because I need to show it as 5.000. How can I accomplish this?


Use a custom format string, e.g.

Console.WriteLine ("{0:0.000}", 5.5);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #3
<"Eric" <nospam>> wrote:
I do not want to use WriteLine. I want to do it inside my business objects.
Is there another way?


Yes - just use String.Format using the same format string, or
yourDouble.ToString("0.000"). My code was only demonstrating the format
string to use.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
Eric wrote:
I do not want to use WriteLine. I want to do it inside my business objects.
Is there another way?

the Double, Float, and Decimal datatypes' ToString() method has an
overload that takes a format string:

myVal.ToString( "0.000");

Thanks

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m...
<"Eric" <nospam>> wrote:
In my application I need to round a number to 3 decimal places. I am
using
the Math.Round method which works fine. The problem I am running into
is
when the number has no decimals like 5 because I need to show it as
5.000.
How can I accomplish this?


Use a custom format string, e.g.

Console.WriteLine ("{0:0.000}", 5.5);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



--
mikeb
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: John Bentley | last post by:
John Bentley writes at this level: If we think about our savings accounts then division never comes in (as far as I can see). We deposit and withdraw exact amounts most of the time. Occasionaly...
1
by: Dr John Stockton | last post by:
When rounding (or truncating) a number which may be either positive or negative, it is sometimes necessary to round either towards plus infinity or towards minus infinity. But those are special...
52
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
19
by: VK | last post by:
http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/ b495b4898808fde0> is more than one month old - this may pose problem for posting over some news servers. This is why I'm...
9
by: clintonb | last post by:
I'm looking for a way to calculate the number of days between two dates using standard C++ functions. Would it be as simple as just using the difftime() function and then dividing that result by...
20
by: jacob navia | last post by:
Hi "How can I round a number to x decimal places" ? This question keeps appearing. I would propose the following solution #include <float.h> #include <math.h>
30
by: bdsatish | last post by:
The built-in function round( ) will always "round up", that is 1.5 is rounded to 2.0 and 2.5 is rounded to 3.0. If I want to round to the nearest even, that is my_round(1.5) = 2 # As...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.