• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Help with C# -- Rounding

Cintirich

Limp Gawd
Joined
Oct 9, 2006
Messages
244
Hi all,

I'm taking a course in C# and I can't figure out how to get my values 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.

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);// pmt interest principle balance);
}
}
}
 
Last edited:
You can pass a format string to a numeric type's ToString
ex:
Code:
Console.WriteLine(pmt.ToString(".##"));

You can also combine format strings with the String.Format method:
Code:
Console.WriteLine(String.Format("{0:.##} {1:.##} {2:.##} {3:.##} ", pmt, interest, principle, balance));
 
are you trying to re-implement a rounding function, or were you having problems with Math.Round()?
 
are you trying to re-implement a rounding function, or were you having problems with Math.Round()?
The problem is that I have no idea how to implement rounding in the first place. I'm not exactly sure where to put the text so that it does what it's supposed to do. My professor said "Look it up" which I have done, but all I can find are 1 line examples, such as Math.Round() but no context as to how to actually fit that into the program that I've written or how to format it.
 
You can pass a format string to a numeric type's ToString
ex:
Code:
Console.WriteLine(pmt.ToString(".##"));
You can also combine format strings with the String.Format method:
Code:
Console.WriteLine(String.Format("{0:.##} {1:.##} {2:.##} {3:.##} ", pmt, interest, principle, balance));
Where, exactly would those go in my code? I'm assuming I would choose one method or the other, not both, correct?
We're really only going into week 2 of this stuff and I'm not sure what I'm doing. (Obviously)

Thank you
 
Just replace the last line, String.Format is a much neater way of building/ presenting strings.

Otherwise your orignal code would look like:

Console.WriteLine(pmt.ToString(".##") + " " + interestpaid.ToString(".##") + " " + interestpaid.ToString(".##") + " " + bal);// pmt interest principle balance);
 
1.) You should declare your variables when you need them, not at the beginning, you're writing c# not Ansi-C
2.) Example for Math.Round:
double a = 2.578;
double b = Math.Round(a, 2);
Console.WriteLine(b);
// output is 2.58;

You should use Math.Round before you actually output the result of your calculations. Using Console.WriteLine("{0:#.##}", a); also outputs 2.58, but usually you should stay away from manually formatting numbers, since it's not culture invariant.
 
You should use Math.Round before you actually output the result of your calculations. Using Console.WriteLine("{0:#.##}", a); also outputs 2.58, but usually you should stay away from manually formatting numbers, since it's not culture invariant.

No idea where you got that, but generally the other way around.

1. Math.Round uses double internally, so you'll get representation issues that return incorrect output. I.e Math.Round(2.135) will round to 2.13. You can't use Math.Round of you want correct output.

2. Your program will be impossible to localize.

String.Format and double.ToString both have overloads that take an IFormatter, so you can pass it the CultureInfo.InvariantCulture if you need to be invariant (which you wouldn't want for a UI)
 
Last edited:
1. I.e Math.Round(2.135) will round to 2.13. You can't use Math.Round of you want correct output.

Math.Round is using Banker's Rounding, which is defined in IEEE 754, which is per definition invariant to culture, since it's an international standard. Most countries I know have exactly the same system, e.g. in germany it's defined in DIN 1333. Cintrich's problem is from that domain, so Math.Round should be just fine and should be the "correct" output.

And yes you certainly should use the IFormatter overloads for the formatting methods, as my example is not using them it's using CultureInfo.CurrentCulture per default, so it's not invariant ;)

So I'm not quite sure why you actually think it's the over way around.
 
1. Math.Round uses double internally, so you'll get representation issues that return incorrect output. I.e Math.Round(2.135) will round to 2.13. You can't use Math.Round of you want correct output.

Banker's rounding *is* correct output.
Otherwise he will want to deliberately encode or specify a non-standard rounding method.
 
are you looking to round the value in your variable or are you just wanting to round for display purposes?

Both actually have been answered, use Math.Round(var, 2);//hundredth
and Console.WriteLine(String.Format("{0:.##} {1:.##} {2:.##} {3:.##} ", pmt, interest, principle, balance));
Which can be simplified, Console.WriteLine("{0:.##} {1:.##} {2:.##} {3:.##} ", pmt, interest, principle, balance);//WriteLine has an overload to accept a format string
 
And yes you certainly should use the IFormatter overloads for the formatting methods, as my example is not using them it's using CultureInfo.CurrentCulture per default, so it's not invariant ;)

So I'm not quite sure why you actually think it's the over way around.
It's the other way around. Why would you want to be invariant, and not obey the Culture setting in the UI? Surely that's the point of having it in the first place.
 
Back
Top