how can I define a string format like this ? 123.190,00 or 23,20 or 100,12 or 1.000,01
I don't need a currency format or ConverterCulture format.
And is it possible to insert the decimal point while the typing ?
Thank you very much
Related
I'm migrating source from vb6.0 to vb.net and am struggling with this format function:
VB6.Format(text, "!#########")
VB6.Format(text, "00000")
I don't understand the meaning of "!#########" and "00000", and how to do the equivalent in VB.Net. Thanks
This:
VB6.Format(text, "!#########")
indicates that the specified text should be left-aligned within a nine-character string. Using standard .NET functionality, that would look like this:
String.Format("{0,-9}", text)
or, using the newer string interpolation, like this:
$"{text,-9}"
The second on is a little bit trickier. It's indicating that the specified text should be formatted as a number, zero-padded to five digits. In .NET, only actual numbers can be formatted as numbers. Strings containing digit characters are not numbers. You could convert the String to a number and then format it:
String.Format("{0:00000}", CInt(text))
or:
String.Format("{0:D5}", CInt(text))
If you were going to do that then it's simpler to just call ToString on the number:
CInt(text).ToString("D5")
If you don't want to do the conversion then you can pad the String explicitly instead:
text.PadLeft(5, "0"c)
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've got a column called Amount, with a lot of numbers looking like this:
67000.00000000000000000000
Some of the columns have 2 numbers after the decimal that need to be retained.
Which should amount to $67,000.00
But my problem is, when I format it into currency or numbers, I get MUCH larger numbers than i would like, looking like this:
6.700.000.000.000.000.000.000.000,00
How can I get it into the right format?
Edit: For this scenario, the user was using ACC2013 and the Field Type was Short Text. The method of conversion that succeeded was : CCur(Val(FieldNameHere))
CCur(YourFieldName)
This will convert it to a currency format.
CLng(YourFieldName)
This will convert it to a long integer format. (It will cut off the decimals)
If you're looking for a reference, Microsoft has a few examples and goes into brief detail about some of these conversion functions.
CCur(Replace("67000.00000000000000000000", ".", Format(0, ".")))
You have to replace point symbol to actual decimal separator before conversion. Because you can't know actual seprator, choosen in regional settings, you have to find it out - and such Format() operation does dirty work.
This code -
Convert.ToDecimal("28.9100000000000000").ToString("c")
will convert a string containing a decimal value to a nicely formatted currency value. Is there anyway to do this without first converting the string value to a decimal?
In order to preserve culture specific currency attributes (currency symbol, separators and precision), your current approach looks like the best one.
If you know the precision and don't care about cultures, you can do some simple string manipulation:
"$" & myString.Substring(0, myString.IndexOf(".") + 3)
A simple method that I use:
MoneyString = string.format("{0:C}",DecimalValue)
How about this?
decimalString = "28.910000000000000000"
currencyString = "$" + decimalString.SubString(0, decimalString.IndexOf('.') + 3)
Of course, if you're going to do this you also need to worry about locale settings. Probably just as easy to do the numeric conversion, and let the framework do the formatting for you.
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