Wrox Programmer Forums
|
C# 4.0 aka C# 2010 General Discussion Discussions about the C# 4.0, C# 4, Visual C# 2010 language and tool not related to any specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 4.0 aka C# 2010 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 21st, 2011, 07:30 PM
Registered User
 
Join Date: Oct 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Rounding

Hi - likely an easy question, but . . .

i successfully round with Math.Round() and get results to 2 decimal places as desired, EXCEPT when the value is accurate to 1 or 0 decimal places, in which case the output is truncated to 1 or 0 decimal places. What i need is to always show 2 decimal places.

Example: If value = 45.233333, the result is 45.33 (perfect)
If value = 45.20 , the result is 45.2 (not good) desired 45.20
If value = 45.00 , the result is 45 (not good) desired 45.00

Suggestions? Thanks!
 
Old October 25th, 2011, 02:52 AM
Wrox Author
 
Join Date: Sep 2010
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
Default

What you need is a display representation. This is done with formats. Check the MSDN docu on "Custom Numeric Format Strings", this should give you the format string you need for displaying a specific number of decimal places.

Hope this helps.
__________________
Christian
CN innovation
Visit my blog at: csharp.christiannagel.com
Follow me on twitter: @christiannagel
 
Old November 7th, 2011, 03:31 AM
Registered User
 
Join Date: Nov 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Number format

You could use the following format:

Console.Write("{0:F2}", value);





Similar Threads
Thread Thread Starter Forum Replies Last Post
Rounding JasperGIS Beginning VB 6 1 December 14th, 2005 03:09 PM
Rounding kilika SQL Server 2000 4 June 1st, 2005 03:36 PM
What's with the rounding?? kaizer BOOK: Beginning Java 2 2 December 22nd, 2003 11:36 PM
Rounding in C# cjo ASP.NET 1.0 and 1.1 Basics 3 November 3rd, 2003 04:12 PM
Rounding Droopy Classic ASP Basics 3 August 14th, 2003 08:45 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.