Heim > Web-Frontend > js-Tutorial > Hauptteil

Wie konvertiere ich in JavaScript effektiv eine Gleitkommazahl in eine Ganzzahl?

Barbara Streisand
Freigeben: 2024-10-18 06:19:02
Original
276 Leute haben es durchsucht

How Do I Convert a Float to an Integer Effectively in JavaScript?

How to Convert a Float to an Integer in JavaScript

Converting a float to an integer in JavaScript is a common task for rounding numeric values. Here are the different methods you can use to perform this conversion efficiently, without relying on string conversion techniques.

Truncation

Truncation rounds a float towards negative infinity, discarding any fractional part. This can be achieved using the Math.floor() method:

<code class="javascript">var intvalue = Math.floor(floatvalue);</code>
Nach dem Login kopieren

Rounding

Rounding rounds a float to the nearest integer. If the fractional part is exactly 0.5, the value is rounded to the nearest even integer. The Math.round() method can be used for this purpose:

<code class="javascript">var intvalue = Math.round(floatvalue);</code>
Nach dem Login kopieren

Ceil

Ceil rounds a float upwards to the nearest integer. This is similar to rounding, except that it always rounds up, regardless of the fractional part. The Math.ceil() method can be used for this:

<code class="javascript">var intvalue = Math.ceil(floatvalue);</code>
Nach dem Login kopieren

ECMAScript 6's Math.trunc()

The ECMAScript 6 standard introduced a new method, Math.trunc(), which explicitly truncates a float to an integer without rounding:

<code class="javascript">var intvalue = Math.trunc(floatvalue);</code>
Nach dem Login kopieren

Bitwise Operators

Bitwise operators can also be used to truncate a float in JavaScript:

  • ~~: This operator performs a bitwise operation that coerces the float to an integer by removing the fractional part.
  • floatvalue | 0 and floatvalue >> 0: These operations also effectively truncate the float to an integer.

Examples

Here are some examples of converting floats to integers using different methods:

Method Positive Float (5.5) Positive Large Float Negative Float (-5.5) Large Negative Float
Math.floor 5 900719925474099 -6 -900719925474100
Math.ceil 6 900719925474100 -5 -900719925474099
Math.round 6 900719925474100 -6 -900719925474100
Math.trunc 5 900719925474099 -5 -900719925474099
~~ 5 858993459 -5 -858993459

Math Object Reference

For more information on the Math object and its methods, refer to the [MDN web docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math).

Das obige ist der detaillierte Inhalt vonWie konvertiere ich in JavaScript effektiv eine Gleitkommazahl in eine Ganzzahl?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Quelle:php
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Neueste Artikel des Autors
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!