Risposte:
Utilizzare Math.round(), possibilmente in combinazione conMidpointRounding.AwayFromZero
per esempio:
Math.Round(1.2) ==> 1
Math.Round(1.5) ==> 2
Math.Round(2.5) ==> 2
Math.Round(2.5, MidpointRounding.AwayFromZero) ==> 3
double d = 1.234;
int i = Convert.ToInt32(d);
Gestisce l'arrotondamento in questo modo:
arrotondato all'intero con segno a 32 bit più vicino. Se il valore è a metà strada tra due numeri interi, viene restituito il numero pari; cioè 4.5 viene convertito in 4 e 5.5 viene convertito in 6.
Math.Roundrestituisce un int come richiesto.
[0,.5)- in basso e esattamente la metà dei numeri - [.5,1)- in alto. L'arrotondamento per inclinare anche leggermente i numeri pari, in quanto arrotonda (.5,1.5)a 1 ma [1.5,2.5]a 2.
Puoi anche usare la funzione:
//Works with negative numbers now
static int MyRound(double d) {
if (d < 0) {
return (int)(d - 0.5);
}
return (int)(d + 0.5);
}
A seconda dell'architettura è più volte più veloce.
double d;
int rounded = (int)Math.Round(d);
So che questa domanda è vecchia, ma mi sono imbattuto nella mia ricerca della risposta alla mia domanda simile. Ho pensato di condividere il consiglio molto utile che mi è stato dato.
Quando si converte in int, aggiungere semplicemente .5al proprio valore prima di effettuare il downcast. Dato che il downcast intscende sempre al numero più basso (ad es. (int)1.7 == 1), Se il tuo numero è .5o più alto, l'aggiunta .5lo porterà al numero successivo e il tuo downcast intdovrebbe restituire il valore corretto. (ad es. (int)(1.8 + .5) == 2)
+ 0.5 * Math.Abs(d)
I metodi in altre risposte vengono generati OverflowExceptionse il valore float è al di fuori dell'intervallo Int. https://docs.microsoft.com/en-us/dotnet/api/system.convert.toint32?view=netframework-4.8#System_Convert_ToInt32_System_Single_
int result = 0;
try {
result = Convert.ToInt32(value);
}
catch (OverflowException) {
if (value > 0) result = int.MaxValue;
else result = int.Minvalue;
}
Per Unity, utilizzare Mathf.RoundToInt .
using UnityEngine;
public class ExampleScript : MonoBehaviour
{
void Start()
{
// Prints 10
Debug.Log(Mathf.RoundToInt(10.0f));
// Prints 10
Debug.Log(Mathf.RoundToInt(10.2f));
// Prints 11
Debug.Log(Mathf.RoundToInt(10.7f));
// Prints 10
Debug.Log(Mathf.RoundToInt(10.5f));
// Prints 12
Debug.Log(Mathf.RoundToInt(11.5f));
// Prints -10
Debug.Log(Mathf.RoundToInt(-10.0f));
// Prints -10
Debug.Log(Mathf.RoundToInt(-10.2f));
// Prints -11
Debug.Log(Mathf.RoundToInt(-10.7f));
// Prints -10
Debug.Log(Mathf.RoundToInt(-10.5f));
// Prints -12
Debug.Log(Mathf.RoundToInt(-11.5f));
}
}
public static int RoundToInt(float f) { return (int)Math.Round(f); }
Sto sviluppando un calcolatore scientifico che sfoggia un pulsante Int. Ho trovato la seguente è una soluzione semplice e affidabile:
double dblInteger;
if( dblNumber < 0 )
dblInteger = Math.Ceiling(dblNumber);
else
dblInteger = Math.Floor(dblNumber);
Math.Round a volte produce risultati imprevisti o indesiderati e la conversione esplicita in numero intero (tramite cast o Convert.ToInt ...) spesso produce valori errati per numeri di precisione più elevata. Il metodo sopra sembra funzionare sempre.
Convert.ToInt32()farà la stessa cosa o spoglia semplicemente tutto dopo il decimale?