如何设置具有两种不同文化/地区格式选项的两个标签的文本?

发布于 2024-08-13 14:05:35 字数 132 浏览 4 评论 0原文

如何设置具有两种不同文化/地区格式选项的两个标签的文本? 第一个标签为ar-EG:阿拉伯语 - 埃及 第二个是 en-US :英语 - 美国

这适用于数字/日期/时间/货币格式。

How does one set text of two labels with two different cultural/region format options?
For first label to be ar-EG : Arabic - Egypt
and second one to be en-US : English - United States ?

This to be done for Number/Date/Time/Currency formats.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

辞取 2024-08-20 14:05:35

在 ToString() 方法中显式使用区域性。例如:

  DateTime dt = DateTime.Now;
  CultureInfo arabic = CultureInfo.GetCultureInfo("ar-EG");
  label1.Text = dt.ToString(arabic.DateTimeFormat);
  CultureInfo english = CultureInfo.GetCultureInfo("en-US");
  label2.Text = dt.ToString(english.DateTimeFormat);

使用 CultureInfo.NumberFormat 格式化数字。

Use the culture explicitly in the ToString() method. For example:

  DateTime dt = DateTime.Now;
  CultureInfo arabic = CultureInfo.GetCultureInfo("ar-EG");
  label1.Text = dt.ToString(arabic.DateTimeFormat);
  CultureInfo english = CultureInfo.GetCultureInfo("en-US");
  label2.Text = dt.ToString(english.DateTimeFormat);

Use CultureInfo.NumberFormat to format numbers.

弱骨蛰伏 2024-08-20 14:05:35

我认为这也许可以解决您的问题:

我需要以两种不同的文化格式展示不同的货币价值。因此,我在每个代码行分配值之后立即执行此操作:

CultureInfo US = new CultureInfo("en-US");
Thread.CurrentThread.CurrentCulture = US;

// Asign your label here

CultureInfo AR = new CultureInfo("ar-EG");  
Thread.CurrentThread.CurrentCulture = AR;

//Asign label here

只需记住将以下命名空间添加到代码文件的顶部:

using System.Threading;
using System.Globalization;

并重新设置以前的区域性。您甚至可以通过在 program.cs 上添加这些行来覆盖系统区域性信息

I think this maybe can solve your problem:

I needed to show different money values in two differents cultural format. So i did this right after each of the code-line asingning the value:

CultureInfo US = new CultureInfo("en-US");
Thread.CurrentThread.CurrentCulture = US;

// Asign your label here

CultureInfo AR = new CultureInfo("ar-EG");  
Thread.CurrentThread.CurrentCulture = AR;

//Asign label here

Just remember to add the folowing namespace to the top of your code-file:

using System.Threading;
using System.Globalization;

and to re-set the previous culture. You can even override the System culture info just by adding those lines on the program.cs

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文