473,796 Members | 2,482 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Round currency

Hi

Have a method that rounds a currency value to the closest quarter. It works
fine as long as the value is above zero, below zero it rounds to the upper
integer. Any working solution is much appreciated.

protected void Page_Load(objec t sender, EventArgs e)
{
double d1 = 2.73d;
Response.Write( LevelCurrency(d 1).ToString("c" )); //Correct

Response.Write( "<br/>");

double d2 = -2.73d;
Response.Write( LevelCurrency(d 2).ToString("c" ));//Incorrect
}

private double LevelCurrency(d ouble price)
{
int totalcost = (int)price;
double d = price - totalcost;
if (d < 0.25d)
return (double)totalco st;

if (d 0.74d)
return (double)(++tota lcost);

return (double)(totalc ost + 0.5);
}

Jul 19 '06 #1
2 3608
Not really sure what your code is doing, but I would do it something like
this:

private double LevelCurrency(d ouble price)
{
int totalcost = (int) price * 4;
return ((double) totalCost / 4.0);
}

This should round to a quarter. You might have to do a check to see if the
value is less than 0 and handle it separate there, though.

Fredo

"Senna" <Se***@discussi ons.microsoft.c omwrote in message
news:40******** *************** ***********@mic rosoft.com...
Hi

Have a method that rounds a currency value to the closest quarter. It
works
fine as long as the value is above zero, below zero it rounds to the upper
integer. Any working solution is much appreciated.

protected void Page_Load(objec t sender, EventArgs e)
{
double d1 = 2.73d;
Response.Write( LevelCurrency(d 1).ToString("c" )); //Correct

Response.Write( "<br/>");

double d2 = -2.73d;
Response.Write( LevelCurrency(d 2).ToString("c" ));//Incorrect
}

private double LevelCurrency(d ouble price)
{
int totalcost = (int)price;
double d = price - totalcost;
if (d < 0.25d)
return (double)totalco st;

if (d 0.74d)
return (double)(++tota lcost);

return (double)(totalc ost + 0.5);
}

Jul 19 '06 #2
* Senna wrote, On 19-7-2006 23:44:
Hi

Have a method that rounds a currency value to the closest quarter. It works
fine as long as the value is above zero, below zero it rounds to the upper
integer. Any working solution is much appreciated.

protected void Page_Load(objec t sender, EventArgs e)
{
double d1 = 2.73d;
Response.Write( LevelCurrency(d 1).ToString("c" )); //Correct

Response.Write( "<br/>");

double d2 = -2.73d;
Response.Write( LevelCurrency(d 2).ToString("c" ));//Incorrect
}

private double LevelCurrency(d ouble price)
{
int totalcost = (int)price;
double d = price - totalcost;
if (d < 0.25d)
return (double)totalco st;

if (d 0.74d)
return (double)(++tota lcost);

return (double)(totalc ost + 0.5);
}
First, you should probably not use a double to hold a currency value, as
doubles have problems with broken amounts and precision. Decimal or
Money types are a better choice. See the MSDN manual for a completer
explanation.

Apart from that I guess you're being bitten by Bankers rounding. .Net
uses this type of rounding, because in the long run it's more honest to
round up and down an even amount of times. This will cause certain
values to be rounded up and other rounded down.

For an explanation on different rounding strategies look here:
http://www.pldesignline.com/howto/sh...leID=175801189

Jesse Houwing
Jul 19 '06 #3

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

Similar topics

2
574
by: Willing 2 Learn | last post by:
I'm still having trouble getting my program to do arithmetic in cents(keeping all #'s) then convert the answer in a format of dollars & cents. The main program should add, subtract, scalar multiply(by int)& show, have a constructor w/ & w/out arguments. Header file should have private data & all 6 functions from above.Class definition file should implement my ADT class. What I have so far: Main program #include "jahcurrency.h" #include...
17
5660
by: nomenklatura | last post by:
Hi, System.Math.Round function is confused me. for example i want to round 3.245 in with decimal symbol Result should be = 3.25 When i try to this in vb: A = 3.245 X = Round(A, 2) then x=3.24 , result is is false
7
4072
by: meenasamy | last post by:
Hi all, i need to create a function that takes three parameters( Original currency, needed currency, amount) i need to convert from the original currency to the needed currency the amount and return the new amount in the needed currency, I need this function to work real time (obtaining real time currency exchange rates), any ideas???
16
9528
by: xjohnx | last post by:
Hi, I'm hoping someone can help me I am quite new to Javascript and have had to create a programme which converts dollars into euros and vice versa, here is my script which is working var currency; var amountEntered; currency = window.prompt('Please enter 0 to convert from Dollars to euros and 1 to convert form euros to dollars',''); currency = parseFloat(currency); while (currency < 0 || currency > 1) { currency =...
3
15988
neo008
by: neo008 | last post by:
Hi all, I am using some variables as "Double" and some text boxes set "DataFormat" property to "Number with two decimal points" for currency calculations. I perform some typical mathematical operations on them and in some cases I see result having more than two decimal points. How can I restrict or round-of to only two decimal places. I'm using VB6.
25
5960
by: mereba | last post by:
Hello My country Ghana is changing its currency. I want to write a small programme in C++ that can covert from the old currency into the new one. I would like this programme to run behind a simple calculator- looking interface. I'm quite new to C++, and I would need some help. Please any suggestion is welcome :) The following is what I have been able to do so far:
1
3293
by: dhutton | last post by:
Hello - Question... What would be the best way to round a number off and change it to currency with the below statement? SELECT Cycle AS BillCycle, SUM(PrevBillAmt) - SUM(Payment) + SUM(Adjustment) + SUM(LocServCharge) + SUM(OtherChgCred) + SUM(TotalTaxes) + SUM(TollCharge) AS Total, **** My result is this: 2241661.6600
7
7502
n8kindt
by: n8kindt | last post by:
using access 2007 here's the code from the builder i'm working with ExpectedTax: (*(-))+(2**) when i was trying this out, one of the results i got was 1.625 ... i need this (and the underlying data) to be rounded to 1.63 ... unfortunately when i tried the round function ExpectedTax: Round((*(-))+(2**),2)
2
1658
by: mahesh123 | last post by:
Hi Folks, I am information regarding the Changing the Indian Currency to USD Currency. How can we change the Indian Currency to USD Currency. For example if the Indian Currency is 100 rs then the USD Currency is the 2.43 dollors. Like that we have to convert the Indian currency to USD Currency. Advance Thanks for Suggestions Mahesh
0
9528
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
10455
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10228
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...
0
10006
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9052
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7547
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
5441
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...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2925
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.