473,503 Members | 3,722 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

(Newbie) Needs help with math equation

bencoding
22 New Member
Hello, I've been learning C# for a few months now and still learning, I come from a PHP background where declaring the variable types is not involved.

I would like to do this simple math problem in C#, is there anyone who can help me?

Number A: Is a monetary value with dollars and cents (eg 199.95)
Number B: Is a numerical percentage value (eg 90%) without the percent sign


My code here returns $17995.50 when the answer I am looking for is $179.95

Expand|Select|Wrap|Line Numbers
  1.  
  2. decimal DollarAmt = 199.95;
  3. int PercentAmt = 90;
  4.  
  5. decimal result = DollarAmt * PercentAmt ;
  6.  
  7. //returns bad result of 17995.50 - help!
  8. Response.Write(result);
Jul 10 '08 #1
4 909
developing
110 New Member
hey

firstly, you should post this in C# section and not in C/C++ section

now, when you look at 90%, you realize that it is 90 of 100 or 90/100.

c# doesnt know that, so you need to divide 'PercentAmt' by 100 by

Expand|Select|Wrap|Line Numbers
  1. PercentAmt /= 100;
also, since you are doing this for the web, i recommend using 'float' or 'double' instead of 'decimal' as they take less memory.

float - 32 bit
double - 64 bit
decimal - 128 bit
Jul 10 '08 #2
bencoding
22 New Member
ok, thanks, I didn't see the C# section before,.... that was helpful and got me one step closer, so when I do this I get a long string past the decimal, is there a round() function that will round it up to the nearest cent? (see my code results)
Expand|Select|Wrap|Line Numbers
  1.         float per = 90;
  2.         float conv_per = p / 100;
  3.  
  4.         double dol = 199.95;
  5.  
  6.         double a = conv_p * dol;
  7.  
  8.         // Returns 179.95499523282 
  9.         Response.Write(a);
Jul 10 '08 #3
bencoding
22 New Member
ah I found this,

Expand|Select|Wrap|Line Numbers
  1. Math.Round(result,2);

works! thanks for the help.
Jul 10 '08 #4
developing
110 New Member
Expand|Select|Wrap|Line Numbers
  1. Math.Round(a, 2);
that should be it.
Jul 10 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

17
3482
by: Just | last post by:
While googling for a non-linear equation solver, I found Math::Polynomial::Solve in CPAN. It seems a great little module, except it's not Python... I'm especially looking for its poly_root()...
8
1994
by: Bshealey786 | last post by:
Okay im doing my final project for my first computer science class(its my major, so it will be my first of many), but anyway im a beginner so im not to great with C++ yet. Anyway this is the error...
4
1416
by: TS | last post by:
I have this simple equation that returns a boolean: if( ti.NumberIncorrect > (TotalQuestions - (TotalQuestions * FailurePercentage) )) ti.NumberIncorrect is int : 4 TotalQuestions is int :...
28
5807
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
39
3519
by: rembremading | last post by:
Hi all! The following piece of code has (for me) completely unexpected behaviour. (I compile it with gcc-Version 4.0.3) Something goes wrong with the integer to float conversion. Maybe somebody...
4
1944
by: sharptool | last post by:
Please, please, please make suggestions on how to resolve these errors. I am trying to build a program that computes a, b, c for the quadratic equation but I'm not having any luck. It's been 5...
1
2141
by: =?ISO-8859-1?Q?Andr=E9?= | last post by:
Would anyone have a "quick and dirty" code sample to create an svg output of a sample math equation using matplotlib? André
2
4837
by: zxry | last post by:
I'm new to C++ and need to write a program that reads and calculates math equations from a char string. My question: is there a way to convert a char math symbol such as '+' into a + the compiler...
2
2378
MrPickle
by: MrPickle | last post by:
I want to take a math equation in a string format then translate this into code. For example; sin(x+2)^4 would translate into: std::pow(4, std::sin(x+2)) but I'm not sure where to start, at...
0
7192
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7064
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5559
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4991
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4665
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3147
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
369
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.