473,499 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Some decimal math.

int sqrft = 1335;
decimal pricePerSqr = 18.33m;
decimal pricePerYear = Math.Round(pricePerSqr * sqrft, 2,
MidpointRounding.AwayFromZero);
decimal pricePerMonth = Math.Round(pricePerYear / 12, 2,
MidpointRounding.AwayFromZero);

Console.WriteLine("/Sqr:"+pricePerSqr.ToString());
Console.WriteLine("/Year:"+pricePerYear.ToString());
Console.WriteLine("/Mth:"+pricePerMonth.ToString());

// Find PricePerSqr given PerMonth.
pricePerMonth = 2039.21m;
pricePerYear = pricePerMonth * 12;
pricePerSqr = Math.Round(pricePerYear / sqrft, 2,
MidpointRounding.AwayFromZero);

Console.WriteLine("-----------");
Console.WriteLine("/Sqr:"+pricePerSqr.ToString());
Console.WriteLine("/Year:"+pricePerYear.ToString());
Console.WriteLine("/Mth:"+pricePerMonth.ToString());

/Sqr:18.33
/Year:24470.55
/Mth:2039.21
-----------
/Sqr:18.33
/Year:24470.52
/Mth:2039.21

Should I be doing something different to make both ways add up the same.
Notice the Year price is slightly different when backing in from
pricePerMonth. TIA

--
William Stacey [MVP]

Jun 8 '06 #1
1 3513
William Stacey [MVP] wrote:
int sqrft = 1335;
decimal pricePerSqr = 18.33m;
decimal pricePerYear = Math.Round(pricePerSqr * sqrft, 2,
MidpointRounding.AwayFromZero);
decimal pricePerMonth = Math.Round(pricePerYear / 12, 2,
MidpointRounding.AwayFromZero);

Console.WriteLine("/Sqr:"+pricePerSqr.ToString());
Console.WriteLine("/Year:"+pricePerYear.ToString());
Console.WriteLine("/Mth:"+pricePerMonth.ToString());

// Find PricePerSqr given PerMonth.
pricePerMonth = 2039.21m;
pricePerYear = pricePerMonth * 12;
pricePerSqr = Math.Round(pricePerYear / sqrft, 2,
MidpointRounding.AwayFromZero);

Console.WriteLine("-----------");
Console.WriteLine("/Sqr:"+pricePerSqr.ToString());
Console.WriteLine("/Year:"+pricePerYear.ToString());
Console.WriteLine("/Mth:"+pricePerMonth.ToString());

/Sqr:18.33
/Year:24470.55
/Mth:2039.21
-----------
/Sqr:18.33
/Year:24470.52
/Mth:2039.21

Should I be doing something different to make both ways add up the same.
Notice the Year price is slightly different when backing in from
pricePerMonth. TIA


Either increase the decimal places of pricePerMonth or ALWAYS calculate
from the lowest unit ie, always start at pricePerMonth (or
perweek/perday/perhour etc..)

HTH
JB
Jun 9 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
7095
by: Maor Mishkin | last post by:
I've changed from double to decimal calculations but I can't find a math library for decimal (mainly for sqrt), Thanks Maor
4
15352
by: vooose | last post by:
Consider a rounding up function: public static decimal RoundUp(decimal val, decimal round) { return ((decimal)Math.Ceiling((double)(val/round)))*round; } Math.Ceiling (and Math.Floor for...
5
23932
by: GaryB | last post by:
I have a database field that is the product of a multiply in a sql statement and it sometimes has 5 or more decimal places. I want to truncate beyond the 2nd decimal place(###.##). I figured a...
4
3646
by: Ryan Liu | last post by:
Is there a good reason that Decimal does not have Ceiling() method, only Math class has? Thanks!
6
536
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
14
2955
by: ray | last post by:
Dear all, In my vb.net program, I have to round a decimal number to two decimal place. For example, for the expression "Decimal.Round(31* 1.555, 2)", I should get the result "48.21". However, the...
11
9980
by: ChrisM | last post by:
Hi Guys, I'm looking for a function that will return the lowest integer that is greater than or equal to a decimal number. eg. 3.1(decimal) returns 4(integer) 3.9(decimal) returns...
13
10449
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
Hi, Why does Math.Sqrt() only accept a double as a parameter? I would think it would be just as happy with a decimal (or int, or float, or ....). I can easily convert back and forth, but I am...
0
7134
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
7180
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
7225
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...
1
6901
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...
0
7392
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5479
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,...
0
3101
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1429
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 ...
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.