473,395 Members | 2,446 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,395 software developers and data experts.

Correct number formatting

I have created a function in the VBA code so that when a string is passed, it will return a number:

Expand|Select|Wrap|Line Numbers
  1. Public Function FN_Test(ByVal LT1 As String) As Single
  2.  
  3. Dim rVal As Single
  4.  
  5. Select Case LT1
  6.     Case "W": rVal = "0.78"
  7.     Case "1": rVal = "1.66"
  8.     Case "L": rVal = "1.66"
  9.     Case "X": rVal = "1.66"
  10.     Case "2C": rVal = "2.17"
  11.     Case "2B": rVal = "2.50"
  12.     Case "2": rVal = "2.50"
  13.     Case "2A": rVal = "2.83"
  14.     Case "3": rVal = "3.32"
  15.     Case "4": rVal = "4.00"
  16.     Case Else:  rVal = Null
  17. End Select
  18.  
  19. FN_Test = rVal
  20.  
Right the problems I am facing... I have to put the rVal numbers into quotes "" because for numbers like 2.50 VBA turns them into 2.5. The number 4.00 gets turned into 4# (very annoying).

I want the number to be returned to my query as a 2 decimal place number - even if it has any 0s after the DP. When I use the FormatNumber(FN_Test('4'), 2) in my query, this bizzarely gets turned into a String so that when I want to add the value to another value, they get concatenated as strings.

If anybody has any idea what I am trying to do and can help, please let me know. I will answer as many questions as I can if you think it will help. Would also appreciate alternative solutions!

Matthew
Mar 5 '09 #1
5 1816
DonRayner
489 Expert 256MB
You could always declare your function as currency rather than single

Expand|Select|Wrap|Line Numbers
  1. Public Function FN_Test(ByVal LT1 As String) As Currency
Mar 5 '09 #2
FishVal
2,653 Expert 2GB
Hello, Matthew.

@MattGaff
Did it really work for you ???

I want the number to be returned to my query as a 2 decimal place number - even if it has any 0s after the DP.
Number data types don't hold format. Variable having value 2.5 and variable having value 2.50 are exactly the same.

When I use the FormatNumber(FN_Test('4'), 2) in my query, this bizzarely gets turned into a String so that when I want to add the value to another value, they get concatenated as strings.
I would make a guess that it is your own code which converts number to string e.g. with Format() function.

If anybody has any idea what I am trying to do and can help, please let me know. I will answer as many questions as I can if you think it will help. Would also appreciate alternative solutions!

Matthew
Number format is an issue of control displaying that number so tune it appropriately. Or convert final result to formatted string.

Kind regards,
Fish.
Mar 5 '09 #3
Stewart Ross
2,545 Expert Mod 2GB
Don's suggestion is an excellent way to achieve what you want to do.

Your current function returns a Single-precision value which should be plenty for what you want to do. You are, however, overly complicating your approach by worrying at the wrong stage about presentation of the result to two decimal places. This is a matter for how the data is presented - it does not change in any way how the value is stored (2.50 is not stored as 2.50 at all - it is stored as a floating-point value which can be converted later to fixed-decimal format for presentation purposes).

For most numeric values you do not need to do anything about the formatting side of things until you present values to users. As most such values are presented in a form or report you simply apply fixed-decimal formatting within the textbox of the form or report concerned.

This approach maintains the highest number of significant digits within any calculations in the processing chain, and therefore the highest possible accuracy of the results shown to users (given that there are always low-level rounding errors involved in floating-point calculations).

-Stewart

ps Apologies Fish for cross-posting - yours was not there when I started this one!
Mar 5 '09 #4
NeoPa
32,556 Expert Mod 16PB
Pretty well everything that needed saying has been said, and well said, already.

I would just bring your attention to line #16 though. If a function is designed to return a Single (or pretty well any specific data type), as opposed to a Variant, then assigning a Null value will cause the program to crash. This is not a valid return value. If this is important, then the function needs to be declared as Variant.

Please pay most attention to Fish's and Stewart's posts though. The most important points are explained there for you.
Mar 5 '09 #5
OldBirdman
675 512MB
According to Access 2000 Help,
FormatNumber Function

Description

Returns an expression formatted as a number.
But no function returns an expression, This function evaluates an expression and returns a string. This string may include digits, the prefix operator "-", parentheses, group delimeters, and decimal points.

Attempts to force the internal representation of the number to store zeros is not going to work. The Currency Data Type is not going to change this. 5 1/2 = five and-a-half = 101.1 in binary. The fractional part of a decimal number may or may not be able to be represented exactly when converted to binary. The Currency Data Type allocates enough places to store the fraction so that accuracy is probably not affected during calculations. For the values presented in the original post, using Currency will not change anything.

I suggest that you keep everything as 'Single'. Remove the quotes in your function, remove the FormatNumber from the queries, and get the values. Do the formatting to display the final results in the control's Format Property, or as the final result in a query.
Mar 5 '09 #6

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

Similar topics

6
by: David Opstad | last post by:
I have a question about text rendering I'm hoping someone here can answer. Is there a way of doing linguistically correct rendering of Unicode strings in Python? In simple cases like Latin or...
16
by: Douglas | last post by:
Gday, How would I format a number so that: TheValue = 32500 Displays in the TextBox as: $32,500.00
102
by: Skybuck Flying | last post by:
Sometime ago on the comp.lang.c, I saw a teacher's post asking why C compilers produce so many error messages as soon as a closing bracket is missing. The response was simply because the compiler...
2
by: Steve Peterson | last post by:
Hi I have an app where I have to deal with both Spanish & American formatting. I have a string that represents a number that I need to convert to Int32 before I enter it in the database. The...
1
by: womblesjc | last post by:
I have a data bound Details View control in asp.net 2.0 that formats a phone number. The 'Default Mode' for the control is set to Edit. The phone number field is a template field and I can...
2
by: dcyale | last post by:
I have a report with the following paragraph: ="This BA presents a " & & " determination for the " & & " and associated habitat. We would appreciate you processing the biological opinion by " &...
109
by: jmcgill | last post by:
Hello. Is there a method for computing the number of digits, in a given numeric base, of N factorial, without actually computing the factorial? For example, 8! has 5 digits in base 10; 10! has...
9
by: Nebojsa4 | last post by:
Hi. First, sorry on my weak English to all. Qusetion: How to read (in VB) Manufacturer serial number of Hard disk drive? Not volume/serial number of C:, D:, etc. partitons. For reading...
2
Pittaman
by: Pittaman | last post by:
Hello I am creating some crystal reports (for visual studio 2005) based on the content of certain .NET objects. I'm doing this in .NET 2.0. For one of them I'm using a Cross-table to summarize...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
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,...

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.