In my system default decimal separator is comma.
In database I have written numeric values as strings but in format with decimal separator point.
Now, when I read data from database like this "3.2" with following code I get value 32D!
NumericUpDown1.Value = CDec(reader("myfield"))
Is here any way that I can get decimal value of 3,2 from showed code in described situation?
You know your number is stored as a string in the format "1,23456". You are dealing with a globalization issue, and as such, you could convert the string using the appropriate culture formatting settings.
NumericUpDown.Value is of type Decimal, so I use the function Convert.ToDecimal.
For this example, I'll assume the number being inserted was stored in the database by a Croatian, so I'd use "hr-HR" for the culture name.
Dim myFieldCroatia As String = "1,2345678901234567890123456789"
NumericUpDown1.Value = Convert.ToDecimal(myFieldCroatia, New Globalization.CultureInfo("hr-HR"))
And if it were stored by an American, I'd use "en-US"
Dim myFieldUnitedStates As String = "1.2345678901234567890123456789"
NumericUpDown2.Value = Convert.ToDecimal(myFieldUnitedStates, New Globalization.CultureInfo("en-US"))
As an aside, if you had first converted to Double, and implicitly converted to Decimal, you would have lost any precision past 15 to 17 decimal places. Not sure if it's required, but it's worthy of noting.
See MSDN for a complete list of culture names
Try this.
NumericUpDown1.Value = CDec(reader("myfield"), new NumberFormatInfo() { NumberDecimalSeparator = "," });
More info
NumericUpDown1.Value = Convert.ToDouble(reader("myfield").ToString().Replace("," , "."))
is another way if you don't want to convert to Double then you could use ToDecimal.
If I were you, I wouldn't use the CDec but the Val function instead.
Try this code :
NumericUpDown1.Value = Val(reader("myfield").ToString().Replace("," , "."))
Related
My software creates PAIN001.XML files directly from an Access financial database. The decimal separator must always be a dot. The numbers are formatted with:
MyText = Format(MyNumber, "#0.00")
However, the format string's dot is automatically replaced by the system decimal separator, which might be "," instead of "." !
In Excel there are easy solutions, for example:
Application.DecimalSeparator = "."
...
However, MS Access doesn't recognize this application property.
Is there a simple way to define a decimal separator within Access vba code ?
Of course, one can create a function which scans each MyText number for wrong decimal separators and replaces them with a dot, but this function would have to be called separately for each number, slowing down the code quite a lot…
The decimal separator must always be a dot.
Then use Str:
MyText = Str(MyNumber)
To convert such a string to a number use Val:
MyNumber = Val(MyText)
I guess the problem is not solveable with the decimal separator Application.DecimalSeparator = ".", even if it was supported by the Access library. It is a rather complicated issue, for the non-US users, as we are used to have , as a decimal separator.
In general, VBA considers only . as a decimal separator. Without taking care of the application default separator, the location of the user and their settings. Thus, some interesting cases could happen:
Sub TestMe()
Dim myText As String
myText = "123,42"
Debug.Print Replace(Format(myText, "#0.00"), ",", ".")
End Sub
A possible solution, that I have implemented some time ago was to use Replace() and to replace as in the gif above. It could be a bit slow indeed, but taking into account the usage of VBA and Access, extreme speed is not something the app could achieve anyway.
I have a series of percentage values saved in a database that look something like this:
Percentage
_____________
100.00000
50.00000
74.02500
When I display the values to the screen, I'd like to trim unnecessary zeroes from the end of the string along with the decimal point so the above examples become:
Percentage
_____________
100
50
74.025
I'm currently using the following code:
displayVal = rawVal.TrimEnd({"0"c, "."c})
but this code continues to trim after the decimal if there are additional zeroes. I also tried:
displayVal = rawVal.TrimEnd(New String({"0", "."}))
which almost works. It just leaves the decimal point.
Is there a way to do what I want using TrimEnd() or do I need to switch to regex?
As Tim already mentioned in the comments, if the data type in the DB is already some numerical type, it would be best to keep it in that type and then use the appropriate numeric formatting when converting it to a string for output. If, however, the input data is already a string, then that's not an option. In that cast, the simplest option is to just do two trims in series, like this:
Private Function RemoveUnecessaryZeros(input As String) As String
Return input.TrimEnd("0"c).TrimEnd("."c)
End Function
However, that doesn't give you a lot of flexibility, it doesn't remove preceding zeros, and it does nothing to reformat the string using the current culture. If that matters, you could instead parse the value into a numeric type and then use the desired string formatting options to re-output it to a string. For instance:
Private Function RemoveUnecessaryZeros(input As String) As String
Dim result As Double
If Double.TryParse(input, result) Then
Return result.ToString()
Else
Return input
End If
End Function
However, when you do it that way, you may potentially lose precision along the way, depending on the input numbers and the data type you choose to parse it with. If you need more control over the parsing/reformatting and you want to keep it purely in strings so no precision is lost, then you may want to consider using regex. For instance:
Private Function RemoveUnecessaryZeros(input As String) As String
Dim m As Match = Regex.Match(input, "[1-9]\d*(\.([1-9]|0+[1-9])+)?")
If m.Success Then
Return m.Value
Else
Return input
End If
End Function
I have a Problem with validating a String in vb.net.
I want to check if the inputString is in a valid standard numeric Format according to http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx except "E" and "X"
So i have the Format specifier e.g. "d4" and my inputString like "1999". But the Specifier is unknown at design time.
Is there a way to validate the inputString if it is valid with the given Format specifier
greetz gangfish
UPDATE:
I am writing an Extension for a working programm.
I am adding massprocessing for selected rows from a gridview.
If a row is selected, the values are shown in my inputFields.
The inputFields are from Telerik UI for WinForms(RadMaskedEditBox).
If multiple rows are selected, i have to remove the Masking, because we have to add an Editor-Pattern for alle Entries (Form: {1..5}test).
So i have to validate if the given input is valid for the masking assigned by the main application. So i have no clue what format specifier is given at design time.
But i know that all standard numeric formats except "E" and "X" are supported by the RadMaskedEditBox
I would like to have something like this:
Dim inputValue = "1999"
Dim formatSpecifier = "d4"
Try
ValidateValue(inputValue, formatSpecifier)
Catch ex As Exception
' Validation Failed. Handle it
End Try
A format specifier is just a string. You can substitute it with any string value at runtime, provided that string value is a valid format specifier. (Otherwise I imagine you'll get an error, so you'll want to have some error handling around all of this.)
For example, given the sample code on the link you provided:
decimal value = 123.456m;
Console.WriteLine(value.ToString("C2"));
If you have a format specifier obtained from somewhere else and stored in a string variable, you can just use that variable:
string formatSpecifier = GetFormatSpecifier();
// ...
decimal value = 123.456m;
Console.WriteLine(value.ToString(formatSpecifier));
Or if it's part of a larger string, you can just concatenate it into that string. So where you would normally have this:
decimal value = 123.456m;
Console.WriteLine("Your account balance is {0:C2}.", value);
You might instead have this:
string formatSpecifier = GetFormatSpecifier();
// ...
decimal value = 123.456m;
Console.WriteLine("Your account balance is " + formatSpecifier + ".", value);
(Or you can construct the string with a StringBuilder. You might also use string.Format() to assemble your format specifier, but I imagine that could introduce confusion when supporting that code.)
How do I convert from a string to an integer? Here's what I tried:
Price = CInt(Int(txtPrice.Text))
I took out the Int and I still got an exception.
Use
Convert.toInt32(txtPrice.Text)
This is assuming VB.NET.
Judging by the name "txtPrice", you really don't want an Integer but a Decimal. So instead use:
Convert.toDecimal(txtPrice.Text)
If this is the case, be sure whatever you assign this to is Decimal not an Integer.
You can try it:
Dim Price As Integer
Int32.TryParse(txtPrice.Text, Price)
You can use the following to convert string to int:
CInt(String) for ints
CDec(String) for decimals
For details refer to Type Conversion Functions (Visual Basic).
Please try this, VB.NET 2010:
Integer.TryParse(txtPrice.Text, decPrice)
decPrice = Convert.ToInt32(txtPrice.Text)
From Mola Tshepo Kingsley (WWW.TUT.AC.ZA)
Convert.ToIntXX doesn't like being passed strings of decimals.
To be safe use
Convert.ToInt32(Convert.ToDecimal(txtPrice.Text))
You can try these:
Dim valueStr as String = "10"
Dim valueIntConverted as Integer = CInt(valueStr)
Another example:
Dim newValueConverted as Integer = Val("100")
Use Val(txtPrice.text)
I would also allow only number and the dot char by inserting some validation code in the key press event of the price text box.
If there might be invalid characters in the textbox it will throw an exception. The Val command pulls numbers and strips invalid characters. It returns a double. So you want to convert the result of Val to whatever type you need.
Price = Convert.toInt32(Val(txtPrice.Text))
This will return 0 instead of throwing an error on invalid input. If that isn't desired you should be checking that the input is valid before you convert.
Yesterday i had a look at how to set values of variables from nummbers stored in external txt files
the variables then needed to be added up so i used trial and error first
((XVAL) + (NEWVAL))
assuming that XVAL was set to 10 and NEWVAL was set to 20 i expected to get the answer of thirty but waqs presented with the new value of 10 20
VB.net pysicaly added the two values together but i wanted the mathematical product of the two which is ((10) + (20)) = 30
yep its a newb question could anyone explain how to achieve what im affter
XVAL and NEWVAL are strings, so they are simply being concatenated together. You need to convert them to integers, so that VB.NET will treat them as such. To do this, use the Int32.Parse() method.
Dim intXVAL As Integer = Int32.Parse(XVAL)
Dim intNEWVAL as Integer = Int32.Parse(NEWVAL)
Dim result = intXVAL + intNEWVAL
You want to cast them to a number first.
Try CDbl.
See http://msdn.microsoft.com/en-us/library/Aa263426 for more.
edit: Oops, thought you were talking about VBA.
Try using Double.Parse(YOURVALUE) if you're talking about VB.NET.
Have you tried the Val() function?
Val(XVAL) + Val(NEWVAL)
The + operator in VB.NET (for backwards-compatibility reasons) means both add and concatenate depending on the types of the variables it is being used with. With two numeric types (Integer, Single, Double, etc.), it adds the values together as you would expect. However, with String types, it concatenates the two strings.
Presumably, then, your XVAL and NEWVAL variables are String types because they're being read out of a text file, which is causing VB.NET to concatenate them into a new string instead of add them together. To get the behavior you're expecting, you need to convert them to numeric types.
Some of the other answers suggest casting simply casting the string values to numeric types (CInt, CSng, CDbl, etc.), but this may not work as expected if the value contained by your string cannot be converted to number. The Int32.Parse method will throw an exception if the value held by your string cannot be represented as a number. This is especially important to keep in mind if you're reading values from a text file that are not guaranteed to adhere to any particular constraints.
Instead, you probably want to use something like Int32.TryParse, which returns a Boolean value indicating whether or not the conversion succeeded and will not throw an exception.
As you are reading from a text file I assume that you are reading your values out as strings, so when you do this:
((XVAL) + (NEWVAL))
It is effectively concatenating the two strings together. In order to get the mathematical product of the two values these need to be int/integers which is the number type.
There are a number of ways you can do this, but in essence you have to 'cast' the strings to ints and then do your calculation.
So in vb.net it would be something like this (pseudo code):
Dim xval As String = "10"
Dim newval As String = "20"
Dim x As Integer = Int32.Parse(xval)
Dim n As Integer = Int32.Parse(newval)
Dim prod As Integer = x + n
Console.WriteLine(prod)
There are a number of other methods of doing this, for example using:
int.Parse(...)
or
Integer.TryParse(...)
More information on these sorts of type conversions can be found here:
http://dotnetperls.com/integer-parse-vbnet
One thing to bear in mind with these sorts of conversions is that you have to be certain that your input data is convertable. Otherwise your code will throw exceptions. This is where TryParse is useful as you can use this to check the inputs and handle invalid inputs without the need for exceptions.