Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB 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 January 17th, 2012, 01:37 PM
Registered User
 
Join Date: Jan 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Help with C# -- Rounding

Hi all,

I'm taking a course in C# and I can't figure out how to get my values (in red) to round to 2 digits. I've searched the internet and I know that Math.round() is the way to do it, in theory, but I can't figure out how to actually implement it into my program, i.e., where to put it and in what format.

If anyone can help, I'd certainly appreciate it!

Thanks,
Rich


namespace ConsoleApplication1
{
class Program
{

static void Main(string[] args)
{
double pv, interestrate, bal, pp, pmt, mir, interestpaid;
int term;

Console.WriteLine("Enter your interest rate in % form ");
interestrate = double.Parse(Console.ReadLine());


Console.WriteLine("Enter your term, in months ");
term = int.Parse(Console.ReadLine());

Console.WriteLine("Enter the amount borrowed ");
pv = double.Parse(Console.ReadLine());

mir = interestrate / 1200;
interestpaid = mir * pv;
pmt = pv * (interestrate / (1 - (1 / ((Math.Pow((1 + mir), term))))));
bal = pv - pmt;
pp = pmt - interestpaid;


Console.WriteLine("Payment Amount Interest Paid Principle Paid New Balance ");
Console.WriteLine(pmt + " " + interestpaid + " " + interestpaid + " " + bal);
}
}
}
 
Old January 17th, 2012, 02:51 PM
Authorized User
 
Join Date: Dec 2011
Posts: 39
Thanks: 9
Thanked 0 Times in 0 Posts
Default

This forum is specifically for the book but this link tells you how to do it.

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


It looks pretty simple to round to nearest.

Code:
Math.Round(4.4); // rounds to 4

Math.Round(4.5); // rounds to 4

Math.Round(4.6); // rounds to 5
Just replacing those hardcoded numbers with whatever variable you want to round should do the trick. I haven't tried this but you should be able to use it inline as well.

Code:
Console.WriteLine(Math.Round(pmt) + " " + Math.Round(interestpaid) 
+ " " + Math.Round(interestpaid ) + " " + Math.Round(bal));
Also, are you supposed to be adding in the interest twice? It looks like you might want to replace the second interestpaid variable with pp instead. At least that what it looks like from your heading.

Last edited by IceThatJaw; January 17th, 2012 at 03:03 PM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
Rounding vanras C# 4.0 aka C# 2010 General Discussion 2 November 7th, 2011 03:31 AM
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





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