0

I have a scenario where I read data(possibly jarray or jobject) from the different files and use them for different file operation which required data with exact precision. However, when I tried to parse the data from the file, all the decimals are rounding off.

Example:

"Price": {
              "CurrencyId": 148,
              "Sales": 1125.0000,
              "Tax": 0.0000,
              "Final": 1125.0000,
              "Final1": 1125.0000,
              "Discount": 0.0,
              "Tip": 0.0,
              "SSG": 0.0,
              "RoundingCorrection": 0.0,
              "DiscountedPrice": null,
              "environmental_fee": 0.0,
              "environmental_fee_label": null,
              "can_be_waived": false
            }

After Parsing:

"Price": {
              "CurrencyId": 148,
              "Sales": 1125.0,
              "Tax": 0.0,
              "Final": 1125.0,
              "Final1": 1125.0,
              "Discount": 0.0,
              "Tip": 0.0,
              "SSG": 0.0,
              "RoundingCorrection": 0.0,
              "DiscountedPrice": null,
              "environmental_fee": 0.0,
              "environmental_fee_label": null,
              "can_be_waived": false
            }

Code Snippet:

public JObject GetJsonData(String directory)
    {
        try
        {
            JObject jObject = JObject.Parse(File.ReadAllText(directory));
            return jObject;
        }
        catch (Exception exp)
        {
            Console.WriteLine("Error Observed in file: "+ directory);
            Console.WriteLine("Error at: "+ exp.Message);
            Console.WriteLine("Error Stacktrace: "+ exp.StackTrace);
        }
        return null;
    }

Is there any way to get exact data from the file without any truncate.

Krishna Barri
  • 1,073
  • 11
  • 23
  • 3
    No. `1125.0000` is the same numeric value as `1125.0`. `0.0` is numerically the same as `0.00000`. If you need to retain the specific numeric representation, you'll need to use a string, or pad the values when outputting them. –  Mar 19 '21 at 19:35
  • 2
    @Amy `decimal` actually preserve zeros on parsing. There is indeed possibility that OP did not show code *because* they are not using `decimal` even if post claims they are .... I.e. `class V { public decimal X; }` and parsing of `var v = JsonConvert.DeserializeObject("{\"X\":1.0000}");` would preserver "1.000" in the `decimal`. – Alexei Levenkov Mar 19 '21 at 20:06
  • @AlexeiLevenkov Interesting. TIL. Thanks. –  Mar 19 '21 at 20:26

2 Answers2

1

I tried this way, it worked.

public static dynamic GetJsonData(String directory)
    {
        var reader = new JsonTextReader(new StringReader(File.ReadAllText(directory)));
        reader.FloatParseHandling = FloatParseHandling.Decimal;
        try
        {
            
            JArray jArray = JArray.Parse(File.ReadAllText(directory));
            return jArray;
        }
        catch (Exception exp)
        {
            var e = File.ReadAllText(directory);
            JObject jObject = JObject.Load(reader);
            return jObject;
        }
        
    }

Thanks, Krishna

Krishna Barri
  • 1,073
  • 11
  • 23
-1

Numbers in js are binary values, which means you should keep them whole numbers. When you get into decimal math, you can't guarantee correct results. Best to keep precision math in C#, and decimals in js as strings.

nichatter
  • 1
  • 3