Home > Blockchain >  Why Newtonsoft.Json.Linq.Item behaves weird
Why Newtonsoft.Json.Linq.Item behaves weird

Time:03-21

Just look at this one line code

jobject.parse("{""volume"": 5074364.34541878}").item("volume").tostring

The result is 5074364,34541878

That is with . instead of comma. How can that be? This has caused lots of bugs.

The value of

Threading.Thread.CurrentThread.CurrentUICulture.NumberFormat.CurrencyDecimalSeparator 

is "."

Recently I found something weird in my computer.

For some reasons many dots becomes comma. For some reasons, some program like firefox think that the decimal separator is comma instead of dot.

That is even though my international settings is set to US (I am actually in Indonesia).

enter image description here

Then the program I created (that should have been independent of this) start behaving erratically.

For example, look at this code

Dim valueInString = grabform.Item(frontstr).ToString

The value of grabform.toString is

{
  "key": "tTRXUSD",
  "bid": 0.060322,
  "ask": 0.060323,
  "last": 0.060289,
  "volume": 5074364.34541878,
  "high": 0.061143,
  "low": 0.060192,
  "base123": "USD",
  "quote123": "TRX"
}

The type of grabform is JObject

in this function

Private Shared Function grabValueOfActualQuote(grabform As JObject, frontstr As String) As Decimal
    Dim valueInString = grabform.Item(frontstr).ToString

    If valueInString = "" Then
        Return 0D
    End If

    If valueInString.Length > 15 Then
        Dim b = 1
    End If
    Dim left1 = Strings.Left(valueInString, 20)
    Dim output = Decimal.Parse(left1, System.Globalization.NumberStyles.Any)

    Return output
End Function

Now the result of valueInstring is 5074364,34541878 (notice the comma)

How can that be?

The value of

enter image description here

It seems that somehow there is a hidden settings in my computer that makes my computer think that the decimal mark is comma instead of dot. Obviously my program should have used dot irrelevant of computer settings. Not to mention I know where the setting is.

Newtonsoft.Json.Linq. shouldn't change behavior no matter what international setting is.

What should I do?

CodePudding user response:

Look at this piece of code:

using System.Globalization;

var xyz = 5074364.34541878;

Console.WriteLine($"Current culture: {CultureInfo.CurrentCulture.Name}");
Console.WriteLine(xyz);

// https://stackoverflow.com/a/5263650/724039 
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture;
Thread.CurrentThread.CurrentUICulture.NumberFormat.CurrencyGroupSeparator = "A";
Thread.CurrentThread.CurrentUICulture.NumberFormat.CurrencyDecimalSeparator = "B";
Thread.CurrentThread.CurrentUICulture.NumberFormat.NumberDecimalSeparator = "C";
Thread.CurrentThread.CurrentUICulture.NumberFormat.NumberGroupSeparator = "D";

Console.WriteLine(xyz.GetType().ToString());

Console.WriteLine(xyz);

Console.WriteLine("Change NumberDecimalSeparator to '.'");
Thread.CurrentThread.CurrentUICulture.NumberFormat.NumberDecimalSeparator = ".";
Console.WriteLine(xyz);

It's output is:

Current culture: nl-NL
5074364,34541878
System.Double
5074364C34541878
Change NumberDecimalSeparator to '.'
5074364.34541878

Conclusion: The value that you read from JSON is a System.Double, and C# applies the NumberDecimalSeparator when using this variable.

  •  Tags:  
  • json
  • Related