I have an application that deals with currency. For display purposes I use the nifty VB FormatCurrency function which will format based on the OS's region setting. So, if in France you might get 123,45 whereas in the US you would get 123.45.
To perform the calculation on these amounts I use CDec() to convert to decimal.
My problem is that when I convert the Decimal to a String using toString() it formats according to the currently set region. I need to be able to always convert the decimal into a String representation for the US, i.e. with decimal points.
I thought I would be able to do something similar to this:
.toString("#0.00")
Try:
value.ToString("C", CultureInfo.InvariantCulture)
More info here:
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx
The "C" is to format for currency, but you can use lots of different format strings. If you really want it formatted for "US" rather than the invariant culture you can do this:
value.ToString("C", CultureInfo.CreateSpecificCulture("en-US"))
This
Dim dec As Decimal = 1.25D
Dim s As String
s = dec.ToString("C2", System.Globalization.CultureInfo.CreateSpecificCulture("en-US"))
produces $1.25
This
s = dec.ToString("N2", System.Globalization.CultureInfo.CreateSpecificCulture("en-US"))
produces 1.25
Try passing InvariantCulture into your ToString() method:
Dim dec As Decimal = 1.25D
dec.ToString(System.Globalization.CultureInfo.InvariantCulture);
//writes 1.25
Related
I get the user machine's date pattern using this:
Dim sysFormat As String = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern
so for example it comes as M/d/yy but in my program I want to parse them in a M/d/yyyy format. But they could even have some other format, we don't know what format. It is ALWAYS gonna be US English tho.
So is there a way to automatically convert whatever it is to the M/d/yyyy format ? or do I have to manually do some string processing code and split the string to different parts for day,month, year?
Have you tried formatting your String to a Datetime object and then parsing using a specific formatter?
Dim myDate As DateTime = DateTime.ParseExact(sysFormat, "M/d/yyyy",
System.Globalization.CultureInfo.InvariantCulture)
And your new String (the one that's formatted) is:
Dim formattedStringDate As String = myDate.ToString("M/d/yyyy")
The requirements you have seem quite specific. You could use DateTime.TryParseExact to try parsing a few valid formats and check whether any results in a valid date. With new C# features you could even get rid of the extra out-parameter declaration. For example:
DateTime.TryParseExact(dateTime,
"M/d/yy",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
DateTime out dt);
I need to make the application work according to system culture. But i have used many hardcoded value for Bit and byte conversion Like Dim maxlimit as double=4.999999.
When i change the cultureInfo for application the bit convertion is not proper. So I am looking for a solution How to use hard coded double values according to the system culture and to manipulate them? (like multiplication, division, addition).
I have used the below snippet to convert the value to Double according to system culture. i have set the Culture to "de-DE"
Code:
Dim l_sDoubleValue As Double
If String.Equals(CultureInfo.CurrentUICulture.ToString(), "de-DE",StringComparison.OrdinalIgnoreCase) Then
l_sDoubleValue = Convert.ToDouble(p_sValue.ToString(CultureInfo.CurrentUICulture), CultureInfo.CurrentUICulture.NumberFormat)
ElseIf String.Equals(CultureInfo.CurrentUICulture.ToString(), "en-US", StringComparison.OrdinalIgnoreCase) Then
l_sDoubleValue = Double.Parse(p_sValue)
End If
Return l_sDoubleValue
Example:
Value = 4.999999
When i convert the value to string using p_sValue.ToString(CultureInfo.CurrentUICulture)
the output is 4,999999 but when i convert the value to Double the output is 4.999999
I'm making a simple calculator code, and when I do the division I want it to show not just the whole number but the decimal number.
This is my division code:
get1.Text = Int(mygive.Text \ rate.Text)
I've also tried:
get1.Text = Int(mygive.Text / rate.Text)
I want it to show numbers like this: 2060.0891
Thanks in advance!
You need to convert the numbers to Double prior to doing the division. CDbl is an appropriate Type Conversion Function for this:
Dim answer = CDbl(mygive.Text) / CDbl(rate.Text)
get1.Text = answer.ToString()
Your Int is converting everything to whole numbers. Try converting to double.
It's showing only the whole part of the number because you're declaring an Integer, which is insufficient, as it can only store whole numbers.
You need to use the Double data type.
Also, you're attempting to apply arithmetic operations on Strings, which is invalid.
You need to convert those strings to be of the type Double before you can do / on them.
Use converting to Double/Decimal
with VB help function:
get1.Text = CDec(mygive.Text / rate.Text).ToString()
with .NET function
get1.Text = Convert.ToDecimal(mygive.Text / rate.Text).ToString()
I am trying to make a conversion tool for different units where i want to display number in exponential form if they exceed 1000(for eg if I/O 10001 then O/P 1.000E+04, if I/O 1000.1 then O/P 1.000E+03).Also for numbers less than 1 (if I/O 0.001 then O/P 1.000E-03, if I/O 9.84203533290685E-04 then O/P 9.842E-04.
Sorry if the qusetion is very simple but I am new to VB .net and pretty bad with maths.
Try using String.Format:
Dim numberToDisplay As Decimal = 12345.00M
Dim formattedNumber As String = String.Format("{0:E3}", numberToDisplay)
'or:
Dim formattedNumber As String = numberToDisplay.ToString("E3")
Then you can use formattedNumber to display where you like.
More info: http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx#EFormatString
You typically just use the ToString() method and pass the desired formatting string to yield your desired result.
Format strings are here:
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx
I am using these codes for displaying time in VB.NET
it shows up in 24 hours format besides i need it in 12 hours format
System.DateTime.Now.Hour
System.DateTime.Now.Minute
System.DateTime.Now.Second
example:
14:12:42
I need it as :
02:12:42
thanks.
Use String.Format. For example:
String.Format("{0:T}", System.DateTime.Now) //02:12:42 PM
String.Format("{0:hh:mm:ss}", System.DateTime.Now) //02:12:42
String.Format("{0:hh:mm:ss tt}", System.DateTime.Now) //02:12:42 PM
Also, this website to be very helpful in summarizing the various ways you can use String.Format. Keep in mind the culture can make a difference on non-custom formats. The first example above using T (Long Time format) works on my US-based PC just fine. But if you say:
String.Format(System.Globalization.CultureInfo.InvariantCulture, _
"{0:T}", System.DateTime.Now)
You end up with 14:12:42. The latter two examples are custom formats and are not affected by culture.
When using DateTime objects you can actually use the ToString() method and set your format inside it.
string currentTime = System.DateTime.Now.ToString("hh:mm:ss");
Check this msdn article out for more clarity:
http://msdn.microsoft.com/en-us/library/zdtaw1bw.aspx
Use the appropriate format string for display.
string formatted = myDateTime.ToString("hh:mm:ss");
I have used a custom format string in this case.
1-Use regex to get first two characters of that string ie from 23:11:59 get 23
2-convert this number to integer type
3-now check it if it is not greater than 12 and if it is subtract 12 from it and by using string.replace replace the old value.
Try This...
Dim CurTime As String
CurTime = TimeOfDay.ToString("h:mm:ss tt")