Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics 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 September 20th, 2004, 08:05 AM
Registered User
 
Join Date: Sep 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Displaying decimals in C#

I'm looking to output to the console a specific number of decimals of a double variable.

Thus, something like 25.1 should be displayed as 25.10.

Is there any easy way to do this?

PS, if it's not obvious, I just started learning C# yesterday.
 
Old September 20th, 2004, 09:38 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
Default

Declare it as a Decimal, not a Double, then use:

FormatNumber([varname], 2)

J
 
Old September 20th, 2004, 12:55 PM
Registered User
 
Join Date: Sep 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm gonna need some clarification on this,

using System;

class test
{
     public static void Main()
     {
          decimal var; //my variable
          var = 25.1;
          Console.WriteLine("var = {0}", FormatNumber(var, 2));
     }
}

should have the output:
var = 25.10

I just generate compiling errors.
 
Old September 20th, 2004, 01:51 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
Default

Sorry, used to VB. Try this:

decimal var=25.1M;
Console.WriteLine(var.ToString("N"));

J





Similar Threads
Thread Thread Starter Forum Replies Last Post
Decimals in charts chetuzz2 Excel VBA 0 July 29th, 2008 02:30 AM
Rounding Numbers and Displaying Decimals elygp Crystal Reports 2 May 8th, 2007 10:56 AM
fractions to decimals mjuliao Access 5 November 2nd, 2005 05:37 PM
more decimals fskilnik Dreamweaver (all versions) 2 September 29th, 2005 07:05 AM
truncating to 2 decimals msrnivas Classic ASP Components 4 July 7th, 2004 02:29 AM





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