I'm trying to display the number formate according to cultureinfo for that
i enter the numbers in text box like 456,456.50 and select the culture english name
as German(Germany) and click the button means it display the given number in the german
format ie 456.456,50 but when i enter the the number as 456.456,50 in text box and select
culture english name as English(United States) it wont display the result. How to i convert
it from 456.456,50 to 456,456.50 where the regional setting is set to English(United States)

Dani AI

Generated

pointed you in the right direction — the issue is parsing, not formatting. The app must parse the textbox text using the culture that matches the input, then format the parsed numeric value using the target culture. In your case the string "456.456,50" uses German separators, so parsing it with en-US will fail. For Windows apps use the System.Globalization APIs explicitly rather than relying on the thread culture.

A robust pattern:

  • If you can, add a "source culture" selector and parse with that culture.
  • If not, try parsing with a short list of likely cultures and fall back to a simple detection heuristic (see code).
  • Always use TryParse with NumberStyles (avoid Parse which throws) and pass an IFormatProvider.

Example VB.NET snippets (short):

Imports System.Globalization

' preferred: parse with a known sourceCulture, then format with targetCulture
Dim sourceCulture As New CultureInfo("de-DE") ' determined from user or UI
Dim targetCulture As New CultureInfo("en-US")
Dim value As Decimal

If Decimal.TryParse(txt.Text.Trim(), NumberStyles.Number Or NumberStyles.AllowCurrencySymbol, sourceCulture, value) Then
  lblResult.Text = value.ToString("N", targetCulture)
End If

If you must auto-detect, try cultures first and then a heuristic when both '.' and ',' appear (the rightmost separator is usually the decimal separator):

' quick heuristic when both separators exist
If input.Contains(".") And input.Contains(",") Then
  Dim decSep = If(input.LastIndexOf("."c) > input.LastIndexOf(","c), "."c, ","c)
  Dim nfi = New NumberFormatInfo() With {
    .NumberDecimalSeparator = decSep.ToString(),
    .NumberGroupSeparator = If(decSep = "."c, ","c, "."c).ToString()
  }
  Decimal.TryParse(input, NumberStyles.Number, nfi, value)
End If

Caveats: strings like "1,234" are ambiguous (thousands vs decimal). Prefer an explicit source-culture control or require unambiguous input. See the .NET docs for details on CultureInfo, NumberFormatInfo, Decimal.TryParse and NumberStyles.

Recommended Answers

All 2 Replies

mohanaraj3485,

Use System.Globalization namespace classes. Which type of application are you developing - Web or Windows?

mohanaraj3485,

Use System.Globalization namespace classes. Which type of application are you developing - Web or Windows?

This for windows application

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.