Skip to content

Getting wrong total amount #1

@txs13

Description

@txs13

I was playing a little bit trying to understand better how it works and in Azure Sandbox program run completely properly. Then I decided to run the same program locally in Visual Studio Code and faced a bug I can't get rid of. The thing is that the calculated total amount is wrong and makes 2012.2000000000003 instead of 2012.2. The same code in Azure Sandbox works properly and I always get the result of 2012.2. The only difference I managed to find is that in Sandbox app it is used netcoreapp3.1 and in the local one - net5.0. But I still can't quite understand how it could impact such a basic functionality. Please find attached the sreenshots. The full code looks like this:

Program result locally in Visual Studio
Program result in Sandbox
DotNetWorkingWithFiles.zip

using System;
using System.IO;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace files_module
{
class Program
{
static void Main(string[] args)
{
var currentDirectory = Directory.GetCurrentDirectory();
var storesDirectory = Path.Combine(currentDirectory,"stores");
var salesTotalDir = Path.Combine(currentDirectory,"salesTotalDir");

        Directory.CreateDirectory(salesTotalDir);

        var salesFiles = FindFiles(storesDirectory);
        var salesTotal = CalculateSalesTotal(salesFiles);
        //Console.WriteLine($"{salesTotal}");
        var totalReportFileName = Path.Combine(salesTotalDir,"totals.txt");

        File.WriteAllText(totalReportFileName,String.Empty);

        foreach (var file in salesFiles)
        {
            string fileSummary = file + " File total is: " + $"{GetJsonTotal(file)}{Environment.NewLine}"; 
            File.AppendAllText(totalReportFileName, fileSummary);
            // Console.WriteLine(fileSummary);
        }

        File.AppendAllText(totalReportFileName,$"Total sales amount: {salesTotal}");
    }

    static IEnumerable<string> FindFiles(string folderName)
    {
        List<string> salesFiles = new List<string>();
        var foundFiles = Directory.EnumerateFiles(folderName, "*", SearchOption.AllDirectories);

        foreach (var file in foundFiles)
        {
            var extension = Path.GetExtension(file);
            // The file name will contain the full path, so only check the end of it
            if (extension == ".json")
            {
                salesFiles.Add(file);
            }
        }   
        return salesFiles;
    }

    class SalesData
    {
        public double Total { get; set; }
    }

    static double CalculateSalesTotal(IEnumerable<string> salesFiles)
    {
        double salesTotal = 0;

        foreach (var file in salesFiles)
        {
            string salesJson = File.ReadAllText(file);
            SalesData data = JsonConvert.DeserializeObject<SalesData>(salesJson);
            salesTotal += data.Total;
            Console.WriteLine($"{data.Total}");
        }
        Console.WriteLine($"Total: {salesTotal}");
        return salesTotal;
    }

    static double GetJsonTotal(string fullJsonFileName)
    {
        string jsonFileContent = File.ReadAllText(fullJsonFileName);
        SalesData data = JsonConvert.DeserializeObject<SalesData>(jsonFileContent);
        return data.Total;
    }
}

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions