English 中文(简体)
与区域环境有关的双重问题
原标题:Issues with the type of double related to regional settings

In my application (WCF, REST, .NET) in an object request I get a value of type double. I need this method to properly fulfilled on numbers with the decimal part "," and ".". How to arrange it without resorting to the type of string. For example: values "3,25" or "3.25" should be treated the same as the type of double.

例如:

这是申请标本中使用的类别。

[DataContract]
public class TripRatesMapped
{
    [DataMember(IsRequired = true)]
    public double Tolls { get; set; }

    [DataMember(IsRequired = true)]
    public double Parking { get; set; }

    [DataMember(IsRequired = true)]
    public double ExtraGr { get; set; }

    [DataMember(IsRequired = true)]
    public double Misc1 { get; set; }

    [DataMember(IsRequired = true)]
    public double Misc2 { get; set; }

}

这是申请对象的一部分。

{ "ExtraGr":1.25 , "Misc1":1.26 , "Misc2":1.27, "Parking":1.28, "Tolls":1.29 }

因此,我可以提出这样的请求:

{ "ExtraGr":1,25, "Misc1":1,26, "Misc2":1,27, "Parking":1,28, "Tolls":1,29 }

如果不使用打字法,这是否可行?

问题回答

如果你使用客户的所在地点,那么你就应当能够把客户的类型转换成可以内部使用的两倍。 在印刷价值时,你需要再次使用当地人。

http://msdn.microsoft.com/en-us/library/3s27fasw.aspx”rel=“nofollow”TryParse 卸载:

string value = "3,25";
NumberStyles style = NumberStyles.Number;
CultureInfo culture = CultureInfo.CreateSpecificCulture("fr-FR");
if (Double.TryParse(value, style, culture, out number))
   Console.WriteLine("Converted  {0}  to {1}.", value, number);
else
   Console.WriteLine("Unable to convert  {0} .", value);

如果你忽略<条码>CultureInfo变量,它将使用当地文化来压缩案文。

通过列入<条码>CultureInfo,在你获得双倍有效或摆脱选择之前,你可以通过尝试不同的文化,在一定程度上增强复原力。 主要的缺点是,如果有人搞错打错,或你以错误的顺序检查文化,那么你会错失。 如果有人类型“35 500”,你首先检查法国文化,然后打上35.5。 而不是<代码>35500.0。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签