Visualizzazione dei risultati da 1 a 4 su 4
  1. #1

    [java] arrotondare valori float

    Che metodo devo usare per arrontondare dei numeri float in numeri interi del tipo:

    5.000001 => 5
    4,999999 => 5

    ?

  2. #2
    Utente di HTML.it L'avatar di Xadoom
    Registrato dal
    Dec 2002
    Messaggi
    1,491
    Mi pare devi usare uno di questi due:
    codice:
     
    ceil
    
    public static double ceil(double a)
    
        Returns the smallest (closest to negative infinity) double value that is not less than the argument and is equal to a mathematical integer. Special cases:
    
            * If the argument value is already equal to a mathematical integer, then the result is the same as the argument.
            * If the argument is NaN or an infinity or positive zero or negative zero, then the result is the same as the argument.
            * If the argument value is less than zero but greater than -1.0, then the result is negative zero.
    
        Note that the value of Math.ceil(x) is exactly the value of -Math.floor(-x).
    
        Parameters:
            a - a value. 
        Returns:
            the smallest (closest to negative infinity) floating-point value that is not less than the argument and is equal to a mathematical integer.
    --------------------------------------------------------
    floor
    
    public static double floor(double a)
    
        Returns the largest (closest to positive infinity) double value that is not greater than the argument and is equal to a mathematical integer. Special cases:
    
            * If the argument value is already equal to a mathematical integer, then the result is the same as the argument.
            * If the argument is NaN or an infinity or positive zero or negative zero, then the result is the same as the argument.
    
        Parameters:
            a - a value. 
        Returns:
            the largest (closest to positive infinity) floating-point value that is not greater than the argument and is equal to a mathematical integer.
    Windows Xp
    [Java]
    [PHP]Notepad++
    [Fortran90-77] elf90 g77
    [C++ /WinAPI] DevC++ VisualC++

  3. #3
    Prova Math.rint()!

  4. #4
    Utente di HTML.it L'avatar di Ed_Bunker
    Registrato dal
    Jul 2003
    Messaggi
    1,119
    Aggiungi 0.5 e casti ad int

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.