Formatting with Decimal.ToString() ... CultureInfo and DigitGrouping...

G

G.Ashok

Hi,

I have created CultureInfo object and specified required digit grouping in
it.
The one of the overloaded ToString methods of Decimal type has parameters to
format the value with required custom format and a IFormatProvider. I pass a
custom format string for positive, negative and Zero (3 sections) and
CultureInfo object containing the required DigitGrouping as IFormatProvider.
But ToString is not formatting the value using the digit grouping specified
in the CultureInfo object I passed. (however it is formatting the value
using 3 sections)

The only problem is... it is not using the DigitGrouping specified in the
CultureInfo. (I have correctly specified the format string to use the digit
grouping)

Any solutions?

Regards,
....Ashok
 
L

Larry Serflaten

G.Ashok said:
Hi,

I have created CultureInfo object and specified required digit grouping in
it.
The one of the overloaded ToString methods of Decimal type has parameters to
format the value with required custom format and a IFormatProvider. I pass a
custom format string for positive, negative and Zero (3 sections) and
CultureInfo object containing the required DigitGrouping as IFormatProvider.
But ToString is not formatting the value using the digit grouping specified
in the CultureInfo object I passed. (however it is formatting the value
using 3 sections)

The only problem is... it is not using the DigitGrouping specified in the
CultureInfo. (I have correctly specified the format string to use the digit
grouping)

Any solutions?


I have no solutions, and I don't work with culture info, (but there were
no other responses, so...) I just thought I would mention that I had
heard that to change the output, the thread's current culture had to be
set. But you seem to be passing in a CultureInfo object to a method.

What happens if you assign your object to the current culture?

System.Threading.Thread.CurrentThread.CurrentCulture = YourCultureInfo
???

LFS
 
C

Cor Ligthert

G. Ashok,

In addition to Larry,

Can you show maybe a piece of code, globalization is very easy, however what
is it what you want to display?

Or what you mean with a custom format string?
What is the culture (and language).
Normally the culture info helps to set a datetime or a value in the
representation of that culture, so how do you want it to be represented.

Maybe after some information about that we can help you.

Cor
 
J

Jay B. Harlow [MVP - Outlook]

Ashok,
It sounds like your custom format string is taking precedence, which makes
sense. As you are saying. "Here is the format I want to use"! Although I'm
not certain that is correct.

What happens when you call ToString supplying only your CultureInfo and a
standard format string? Do you get the standard format or your "digit
grouping".

As Cor asked, can you post the actual code you are using (10 to 15 lines
that demonstrates the problem is all we need). As I'm not seeing a
"DigitGrouping" property in CultureInfo or NumberFormatInfo.

Hope this helps
Jay
 
L

Larry Serflaten

Rajesh Nandwani said:
I am attaching sample. If you run the sample you will found 2 TextBoxes.
The first one in which I am interested. The second one has a standard
currency string and shows the result correctly.


It would help if you indicate what your desired result was.

When I enter 123456 and then leave, I see: Rs. 123,456.00
I don't know if that is what you expect or not....

LFS
 
L

Larry Serflaten

Rajesh Nandwani said:
I am attaching sample. If you run the sample you will found 2 TextBoxes.
The first one in which I am interested. The second one has a standard
currency string and shows the result correctly.

Try this:

Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)

Me.MyCultureInfo.NumberFormat.CurrencyGroupSizes = New Integer() {3, 2}
Threading.Thread.CurrentThread.CurrentCulture = Me.MyCultureInfo

End Sub


Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave

If Not IsNumeric(Me.TextBox1.Text) Then
Me.TextBox1.Text = "0"
End If

Me.TextBox1.Text = CDec(Me.TextBox1.Text).ToString("C")

End Sub
 
G

G.Ashok

Dear Larry,

This does show the grouping if you include a standard currency format string
(That is what i showed in my sample in second TexBox2). But I want to set my
own format string (In TextBox1)

For example I wnat to my own format string like

Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TextBox1.Leave

If Not IsNumeric(Me.TextBox1.Text) Then
Me.TextBox1.Text = "0"
End If

Me.TextBox1.Text = CDec(Me.TextBox1.Text).ToString("'Rs.'#,##0.00
CR;'Rs.'#,##0.00 DB;Zero")

End Sub

Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)

Me.MyCultureInfo.NumberFormat.CurrencyGroupSizes = New Integer() {3,
2}
Threading.Thread.CurrentThread.CurrentCulture = Me.MyCultureInfo

End Sub

Since I want to use the GroupingSizes (DigitGrouping) of the NumberFormat
in/with my custom format string as specified above, I have set my required
Currency Digit Grouping (CurrencyGroupSizes = New Integer() {3, 2}) and
passing in the parameter of the ToString() like this.

Me.TextBox1.Text = CDec(Me.TextBox1.Text).ToString("'Rs.'#,##0.00
CR;'Rs.'#,##0.00 DB;Zero", MyCultureInfo)

But the problem is it is not get used in the formatting of the above lline.

In short I want to specifiy a digit grouping (3,2) and it get it used in my
custom format string passed to Decimal's ToString().

Any Ideas?

Regards,
....Ashok


Larry Serflaten said:
Try this:

Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)

Me.MyCultureInfo.NumberFormat.CurrencyGroupSizes = New Integer() {3, 2}
Threading.Thread.CurrentThread.CurrentCulture = Me.MyCultureInfo

End Sub


Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TextBox1.Leave
 
J

Jay B. Harlow [MVP - Outlook]

Rajesh,
Can you post 10 to 15 of code inline (cut & paste) in your message, not an
entire project in a zip file.

Along with the desired result as Larry stated...

For some reason I am not able to read your zip file.

Hope this helps
Jay

Rajesh Nandwani said:
Thanks,

I am attaching sample. If you run the sample you will found 2 TextBoxes.
The first one in which I am interested. The second one has a standard
currency string and shows the result correctly.

Regards,
...Ashok
 
G

G.Ashok

Jay,

Here the sample.

Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TextBox1.Leave

If Not IsNumeric(Me.TextBox1.Text) Then
Me.TextBox1.Text = "0"
End If

Me.TextBox1.Text = CDec(Me.TextBox1.Text).ToString("'Rs.'#,##0.00
CR;'Rs.'#,##0.00 DB;Zero")

End Sub

Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)

Me.MyCultureInfo.NumberFormat.CurrencyGroupSizes = New Integer()
{3,2}
Threading.Thread.CurrentThread.CurrentCulture = Me.MyCultureInfo

End Sub

Since I want to use the GroupingSizes (DigitGrouping) of the NumberFormat
in/with my custom format string as specified above, I have set my required
Currency Digit Grouping (CurrencyGroupSizes = New Integer() {3, 2}) and
passing in the parameter of the ToString() like this.

Me.TextBox1.Text = CDec(Me.TextBox1.Text).ToString("'Rs.'#,##0.00
CR;'Rs.'#,##0.00 DB;Zero", MyCultureInfo)

But the problem is it is not get used in the formatting of the above lline.

In short I want to specifiy a digit grouping (3,2) and it get it used in my
custom format string passed to Decimal's ToString().

Any Ideas?

Regards,
....Ashok
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top