Aller au contenu | Aller au menu | Aller à la recherche


arrondi float double Decimal BigDecimal

Comment les types arrondissent les valeurs décimales dans les langages JavaScript, Node.js, Java, Csharp ...

Sur un Windows 64 bits

Node.js

C:\Users\ryzen>node
Welcome to Node.js v14.9.0.
Type ".help" for more information.
> .1+.2
0.30000000000000004
> 2.89+3.21
6.1
> 2.89+3.2126
6.102600000000001
  • bigdecimal - BigDecimal for Javascript is a pure-Javascript implementation of immutable, arbitrary-precision, signed decimal numbers. BigDecimal supports decimal math with arbitrary precision.

JavaScript, sur les navigateurs Edge, Chrome, Firefox > Console

.1+.2
0.30000000000000004

2.89+3.21
6.1

2.89+3.2126
6.102600000000001

Java

a : .1 + b : .2 = ? ...
Float:      0.3
Double:     0.30000000000000004
BigDecimal: 0.3
* 
a : 2.89 + b : 3.21 = ? ...
Float:      6.1000004
Double:     6.1
BigDecimal: 6.10
* 
a : 2.89 + b : 3.2126 = ? ...
Float:      6.1026
Double:     6.102600000000001
BigDecimal: 6.1026
package fr.placeoweb;

import java.math.BigDecimal;
import org.apache.commons.lang3.StringUtils;

public class TestFloatDoubleBigDecimal {
    
    public static void main(String[] args) {
        
        TestFloatDoubleBigDecimal test = new TestFloatDoubleBigDecimal();        
                
        System.out.println("a : " + test.a + " + b : " + test.b + " = ? ...");
        
        System.out.println(StringUtils.rightPad("Float: ", 12, " ") + test.testFloat());
        System.out.println(StringUtils.rightPad("Double: ", 12, " ") + test.testDouble());
        System.out.println(StringUtils.rightPad("BigDecimal: ", 12, " ") + test.testBigDecimal());

    }
    
    private String a = "0.01";
    private String b = "0.68";
    
    public float testFloat() {
        float a, b, c;
        a = Float.parseFloat(this.a);
        b = Float.parseFloat(this.b);        
        c = a+b;
        return c;
    }
    
    public double testDouble() {
        double a, b, c;        
        a = Double.parseDouble(this.a);
        b = Double.parseDouble(this.b);        
        c = a+b;
        return c;
    }
    
    public BigDecimal testBigDecimal() {
        BigDecimal a, b, c;
        a = new BigDecimal(this.a);
        b = new BigDecimal(this.b);
        c = a.add(b);
        return c;
    }
    
}

dotnet/csharp .NET/C#

a: .1 + b : .2 = ? ...
float   : 0.3
double  : 0.30000000000000004
decimal : 0.3

a: 2.89 + b : 3.21 = ? ...
float   : 6.1000004
double  : 6.1
decimal : 6.10

a: 2.89 + b : 3.2126 = ? ...
float   : 6.1026
double  : 6.102600000000001
decimal : 6.1026
using System;
using System.Globalization;

namespace ConsoleAppNetCore
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Globalization.CultureInfo customCulture = (System.Globalization.CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
            customCulture.NumberFormat.NumberDecimalSeparator = ".";

            System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;
            CultureInfo myCIintl = customCulture;

            float floatA = float.Parse(a, myCIintl); // .1f;
            float floatB = float.Parse(b, myCIintl); // .2f;

            double doubleA = double.Parse(a, myCIintl); // .1
            double doubleB = double.Parse(b, myCIintl); // .2;

            decimal decimalA = Decimal.Parse(a, myCIintl); // new Decimal(.1);
            decimal decimalB = Decimal.Parse(b, myCIintl); // new Decimal(.2);

            Console.WriteLine("a : " + a + " + b : " + b + " = ? ...");
            Console.WriteLine("float   : " + (floatA + floatB));
            Console.WriteLine("double  : " + (doubleA + doubleB));
            Console.WriteLine("decimal : " + (decimalA + decimalB));
        }
    }
}

Ajouter un commentaire

Le code HTML est affiché comme du texte et les adresses web sont automatiquement transformées.

Fil des commentaires de ce billet