473,655 Members | 3,105 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 922
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
3514
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() functionality (which solves arbitrary polynomials). Does anyone know of a Python module/package that implements that? Just
8
2000
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 msg that im getting: "Error executing cl.exe" this is the code that I have, but I know whats causing it, ill just show you the whole thing first though //file: Quadratic
4
1422
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 : 20 FailurePercentage is float : 80 When the code runs, it evaluates to true incorrectly. When I put this exact expression in the watch or immediate window, it evaluates correctly to
28
5867
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places? ----------------------------------------------------------------------- When formatting money for example, to format 6.57634 to 6.58, 6.5 to 6.50, and 6 to 6.00? Rounding of x.xx5 is uncertain, as such numbers are not represented exactly. See section 4.7 for Rounding issues.
39
3552
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 out there understands what happens. Essentially, when I subtract the (double) function value GRID_POINT(2) from a variable which has been assigned the same value before this gives a non-zero result and I really do not understand why.
4
1955
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 hours and I've only manage to clear 4 of my errors. Please note the primary focus is to use function calls. The main function needs to pass the coefficients to a function root1 that computes and returns the value of x1 to the main. I am trying to do...
1
2155
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
4846
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 can use in an equation? Right now I'm using a bunch of If statements to handle this, but would like to know if there's a way to work the char sign directly into the equation. i.e. char num1 = '5'; char sign = '+'; char num2 = '2';
2
2388
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 first I decided to follow the order of operations, breaking down the equation and putting it into an array this gave me two problems a) Do I remove the selection from the string as I add it to the array? b) I wouldn't know where to go from there. ...
0
8380
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8296
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8710
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8497
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6162
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2721
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1928
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1598
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.