using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Collections.Specialized;

/// <summary>
/// Descrizione di riepilogo per Function
/// </summary>
public static class LibFunction
{
    public enum tipoArrotondamento
    {
        eccesso,
        difetto,
        arbitrario
    }

    /// <summary>
    /// Funzione per arrotondare i valori sugli assi dei grafici
    /// </summary>
    /// <param name="valore">Valore numerico da arrotondare</param>
    /// <param name="tipo">Tipo di arrotondamento ad es. per eccesso forza l'arrotondamento sempre per eccesso</param>
    /// <returns>Restituisce un double</returns>
    public static double ArrotondaAxis (double valore, tipoArrotondamento tipo)
    {
        double tempValue;
        int peso;
        double results = 0.0;
        int ordineGrandezza;
        int pesoArrotondamento = 1;
        bool neg = false;

        if (valore == 0)
            return 0;
        else if (valore < 0)
        {
            neg = true;
            valore = Math.Abs(valore);
        }


        ordineGrandezza = Convert.ToString(Math.Truncate(Math.Abs(valore))).Length;

        if (ordineGrandezza == 5)
            peso = 1;
        else if (ordineGrandezza == 6)
            peso = 2;
        else if (ordineGrandezza > 6)
            peso = 3;
        else
            peso = 0;

        if (ordineGrandezza > 2)
        {
            pesoArrotondamento = Convert.ToInt32(Math.Pow(10.0, Convert.ToDouble(ordineGrandezza - peso - 1)));
            tempValue = valore / Convert.ToDouble(pesoArrotondamento);
        }
        else
        {
            pesoArrotondamento = 1;
            tempValue = valore;
        }

        switch (tipo)
        {
            case tipoArrotondamento.eccesso:
                if (tempValue==1)
                    results = Math.Round(tempValue, MidpointRounding.AwayFromZero) * pesoArrotondamento;            
                else                
                    results = Math.Round(tempValue + 0.5, MidpointRounding.AwayFromZero) * pesoArrotondamento;

                break;
            case tipoArrotondamento.difetto:
                results = Math.Floor(tempValue) * pesoArrotondamento;
                break;
            default:
                results = Math.Round(tempValue, MidpointRounding.AwayFromZero) * pesoArrotondamento;
                break;
        }

        if (neg)
            return results * (-1);
        else
            return results;
    }


    /// <summary>
    /// Arrotonda le cifre percentuali 
    /// </summary>
    /// <param name="valore">Valore numerico da arrotondare</param>
    /// <param name="tipo">Tipo di arrotondamento ad es. per eccesso forza l'arrotondamento sempre per eccesso</param>
    /// <returns></returns>
    public static double ArrotondaPercentuale (double valore, tipoArrotondamento tipo)
    {
        double tempValue;
        double results = 0.0;
        bool neg = false;

        if (valore == 0)
            return 0;
        else if (valore < 0)
        {
            neg = true;
            valore = Math.Abs(valore);
        }

        tempValue = Math.Truncate(Math.Abs(valore));

        switch (tipo)
        {
            case tipoArrotondamento.eccesso:
                results = Math.Round(tempValue + 0.5, MidpointRounding.AwayFromZero);
                break;
            case tipoArrotondamento.difetto:
                results = Math.Floor(tempValue - 0.5);
                break;
            default:
                results = Math.Ceiling(tempValue);
                break;
        }

        if (neg)
            return results * (-1);
        else
            return results;
    }
}