473,398 Members | 2,525 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Trying to round to a double

219 100+
I'm trying to do some rounding of decimal values I have. They are prices, which need to be rounded up to the next nickel.

Examples:

.23 = .25
.01 = .05
3.21 = 3.25
.07 = .10
6.28 = 6.30

I created a function, but I can't use this line of code so I'm not sure how I can do this now. I get an error that I'm trying to divide by 0.

If VARIABLE Mod .05 then
'Price not divisible by .05 ... round to next nickel
else
'Price is divisible by .05
endif
Oct 11 '07 #1
1 1253
ADezii
8,834 Expert 8TB
I'm trying to do some rounding of decimal values I have. They are prices, which need to be rounded up to the next nickel.

Examples:

.23 = .25
.01 = .05
3.21 = 3.25
.07 = .10
6.28 = 6.30

I created a function, but I can't use this line of code so I'm not sure how I can do this now. I get an error that I'm trying to divide by 0.

If VARIABLE Mod .05 then
'Price not divisible by .05 ... round to next nickel
else
'Price is divisible by .05
endif
The following Function will accept a SINGLE Data Type and return a properly formatted STRING Round Up to the next Nickel, if not evenly divisible by .05. I did this prior to bedtime, so it comes with no guarantees:
Expand|Select|Wrap|Line Numbers
  1. Public Function fRoundUpToNickel(sngValue As Single) As String
  2.   If Not IsNumeric(sngValue) Or IsNull(sngValue) Then Exit Function
  3.  
  4.   sngValue = sngValue * 100     'get rid of Decimal for now
  5.   If sngValue Mod 5 = 0 Then
  6.     fRoundUpToNickel = Format(sngValue / 100, "#,#,#,#.00")
  7.   Else
  8.     fRoundUpToNickel = Format(((5 - (sngValue Mod 5)) + sngValue) / 100, "#,#,#,#.00")
  9.   End If
  10. End Function
  11.  
Sample OUTPUTs in Debug Window
Expand|Select|Wrap|Line Numbers
  1. ? fRoundUpToNickel(16.00)
  2. 16.00
  3.  
  4. ? fRoundUpToNickel(.23)
  5. .25
  6.  
  7. ? fRoundUpToNickel(.01)
  8. .05
  9.  
  10. ? fRoundUpToNickel(3.21)
  11. 3.25
  12.  
  13. ? fRoundUpToNickel(.07)
  14. .10
  15.  
  16. ? fRoundUpToNickel(6.28)
  17. 6.30
  18.  
  19. ? fRoundUpToNickel(1245876.99)
  20. 1,245,877.00
Oct 11 '07 #2

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

Similar topics

5
by: JustSomeGuy | last post by:
In MS Visual C++ 6.0 Where is M_PI defined? Where is round defined?
6
by: Jef Driesen | last post by:
I need to implement a function to implement the rounding of floating point values. At the moment i have two different implementations, depending on the type of the return value (integer or double)....
6
by: Penguin | last post by:
At some long ago time Steve Jorgensen answered thus: Subject: Re: How can I round a time? Newsgroups: comp.databases.ms-access Date: 1998/12/11 Access represents a date internally as a double...
3
by: ORC | last post by:
Is there a round function that do a "Normal" rounding and not the round to nearest as the Math.Rounds is? Math.Round(3.44, 1); //Returns 3.4. Math.Round(3.45, 1); //Returns 3.4....
6
by: ng_mr | last post by:
No, not a question about "banker's rounding" or whatever it's called. I want to round a double to the nearest 100th, so I perform the following: // original is a double double result =...
9
by: Ronald W. Roberts | last post by:
I'm having a problem understanding the Round function. Below are quotes from two books on VB.NET. The first book shows examples with one argument and how it rounds. The second book something...
21
by: Karl O. Pinc | last post by:
FYI, It'd be nice if the error message from a REFERENCES constraint mentioned the column name into which the bad data was attempted to be inserted. In PostgreSQL 7.3: sandbox=> insert into...
5
by: ibiza | last post by:
Hi all, I'd need a function that would rounds values like : d = roundTo(64.2, 0.25) ' returns 64.25 d = roundTo(64.2, 10) ' returns 64 d = roundTo(64.2, 100) ' returns 100 well, that rounds...
0
by: tangoal | last post by:
I have a table1 as: id(int); percent(double,2); a(double,2); b(double,2); c(double,2);.....z(double,2) 1; 0.12; 1.33; 1.26; 1.89;........ 2; 0.11; 1.55; 1.22; 1.56;........ : : and many...
4
by: =?Utf-8?B?UmVuZQ==?= | last post by:
Hello everyone I have a problem with Math.Round, it´s ocurring some strange: Math.Round(12.985) = 12.98, it´s wrong. It should be: 12.99 Why?? What is the problem? Help ME !!!!
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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
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
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...
0
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
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
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
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...

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.