Cookie Consent by Free Privacy Policy Generator Aktuallisiere deine Cookie Einstellungen ๐Ÿ“Œ JavaScript โ€“ Rounding floating point numbers


๐Ÿ“š JavaScript โ€“ Rounding floating point numbers


๐Ÿ’ก Newskategorie: Programmierung
๐Ÿ”— Quelle: dev.to

There are six different functions in JavaScriptโ€™s Math library for rounding floating point numbers:

  • abs()
  • ceil()
  • floor()
  • round()
  • trunc()
  • fround()

What is the difference between these functions?

abs()

abs() is the absolute value of a number. Mathematically speaking, it is the distance of a number from zero. As a distance cannot be negative, the absolute value is always the positive value of a number:

Math.abs(7.89012) = 7.89012
Math.abs(-7.89012) = 7.89012

ceil()

ceil() rounds up a number to the nearest integer value greater than or equal to the given number:

Math.ceil(7.1) = 8
Math.ceil(7.8) = 8
Math.ceil(-7.1) = -7
Math.ceil(-7.8) = -7

Please note that the next largest integer from -7.1 is not -8, but -7.

floor()

floor() is the counterpart to ceil(), so it rounds the given number to the next smaller or equal number to the given number:

Math.floor(7.1) = 7
Math.floor(7.8) = 7
Math.floor(-7.1) = -8
Math.floor(-7.8) = -8

round()

round() rounds the given number to the nearest whole number:

Math.round(7.1) = 7
Math.round(7.8) = 8
Math.round(-7.1) = -7
Math.round(-7.8) = -8

The function works mathematically correctly:

math.round(7.49) = 7
math.round(7.5) = 8

trunc()

Math.trunc(7.1) = 7
Math.trunc(7.8) = 7
Math.trunc(-7.1) = -7
Math.trunc(-7.8) = -7

fround()
This function is only required in special situations where you have to work with 32-bit numbers, e.g. in graphics or audio programming. It enables more precise rounding than the more general round().
Here is a more detailed explanation than is useful in this article.

(Foto von Mika Baumeister auf Unsplash)

...



๐Ÿ“Œ JavaScript โ€“ Rounding floating point numbers


๐Ÿ“ˆ 71.53 Punkte

๐Ÿ“Œ A Guide to Rounding Numbers in JavaScript


๐Ÿ“ˆ 45.88 Punkte

๐Ÿ“Œ Dealing with Floating Point Numbers in JavaScript: Lessons Learned


๐Ÿ“ˆ 43.16 Punkte

๐Ÿ“Œ Kasr sonlar haqida (fractional numbers)(Floating-point numeric types (C# reference))


๐Ÿ“ˆ 36.49 Punkte

๐Ÿ“Œ Medium CVE-2020-8597: Point-to-point protocol project Point-to-point protocol


๐Ÿ“ˆ 34.49 Punkte

๐Ÿ“Œ โ›ต Sailing the Choppy Waters of Floating-Point Precision in JavaScript ๐Ÿ”ข


๐Ÿ“ˆ 32.32 Punkte

๐Ÿ“Œ Exploring JavaScript's Floating Point Arithmetic: Why (0.1 + 0.2) !== 0.3


๐Ÿ“ˆ 32.32 Punkte

๐Ÿ“Œ [dos] Apple iOS/macOS - NSKeyedArchiver Heap Corruption Due to Rounding Error in 'TIKeyboardLayout initWithCoder:'


๐Ÿ“ˆ 28.37 Punkte

๐Ÿ“Œ [dos] Skia Graphics Library - Heap Overflow due to Rounding Error in SkEdge::setLine


๐Ÿ“ˆ 28.37 Punkte

๐Ÿ“Œ Rounding Up Your IoT Security Requirements: Draft NIST Guidance for Federal Agencies


๐Ÿ“ˆ 28.37 Punkte

๐Ÿ“Œ CodeSOD: Rounding Percentages


๐Ÿ“ˆ 28.37 Punkte

๐Ÿ“Œ Microsoft is rounding up Edge with a new image-magnifying feature


๐Ÿ“ˆ 28.37 Punkte

๐Ÿ“Œ NSS Buffer Overflows / Floating Point Exception


๐Ÿ“ˆ 25.65 Punkte

๐Ÿ“Œ Lazy FPU: Intels Floating Point Unit kann geheime Daten leaken


๐Ÿ“ˆ 25.65 Punkte

๐Ÿ“Œ Lazy FPU: Intels Floating Point Unit kann geheime Daten leaken


๐Ÿ“ˆ 25.65 Punkte

๐Ÿ“Œ Intels Floating Point Unit kann geheime Daten leaken


๐Ÿ“ˆ 25.65 Punkte

๐Ÿ“Œ Chrome Swiftshader Blitting Floating-Point Precision Errors


๐Ÿ“ˆ 25.65 Punkte

๐Ÿ“Œ Chrome Swiftshader Blitting Floating-Point Precision Errors


๐Ÿ“ˆ 25.65 Punkte

๐Ÿ“Œ [dos] Google Chrome - Swiftshader Blitting Floating-Point Precision Errors


๐Ÿ“ˆ 25.65 Punkte

๐Ÿ“Œ RTX-3000-Technik im Detail: Floating Point ist Amperes Liebling


๐Ÿ“ˆ 25.65 Punkte

๐Ÿ“Œ CVE-2022-32981 | Linux Kernel up to 5.18.3 on PowerPC 32-bit Floating Point Register ptrace-fpu.c ptrace_get_fpr buffer overflow


๐Ÿ“ˆ 25.65 Punkte

๐Ÿ“Œ CVE-2022-4293 | vim prior 9.0.0804 floating point comparison with incorrect operator


๐Ÿ“ˆ 25.65 Punkte

๐Ÿ“Œ Floating Point Exception C++ Why and what is it?


๐Ÿ“ˆ 25.65 Punkte

๐Ÿ“Œ When 0.2 + 0.1 Isn't Quite 0.3: A Comedic Guide to Floating-Point Shenanigans


๐Ÿ“ˆ 25.65 Punkte

๐Ÿ“Œ CVE-2022-46487 | Scontain SCONE up to 5.7.x Floating Point __scone_entry observable behavioral discrepancy


๐Ÿ“ˆ 25.65 Punkte

๐Ÿ“Œ Has the Decades-Old Floating Point Error Problem Been Solved?


๐Ÿ“ˆ 25.65 Punkte

๐Ÿ“Œ Bounded Floating Point: Patent grenzt Genauigkeitsfehler bei FlieรŸkommazahlen ein


๐Ÿ“ˆ 25.65 Punkte

๐Ÿ“Œ CVE-2007-6745 | ClamAV 0.91.2 ScanOLE2 Floating Point Remote Code Execution (OSVDB-80988)


๐Ÿ“ˆ 25.65 Punkte

๐Ÿ“Œ CVE-2018-3665: Floating Point Lazy State Save/Restore vulnerability affects Intel chips


๐Ÿ“ˆ 25.65 Punkte

๐Ÿ“Œ AVX2 floating point improvements in Visual Studio 2019 version 16.5


๐Ÿ“ˆ 25.65 Punkte

๐Ÿ“Œ Bugtraq: APPLE-SA-2017-03-27-1 Pages 6.1, Numbers 4.1, and Keynote 7.1 for Mac; Pages 3.1, Numbers 3.1, and Keynote 3.1 for iOS


๐Ÿ“ˆ 21.68 Punkte

๐Ÿ“Œ Do You Know Your Numbers?  No, Your Cyber Health Numbers!


๐Ÿ“ˆ 21.68 Punkte

๐Ÿ“Œ How Do I Combine Text and Numbers, and Then Make the Numbers Incremental?


๐Ÿ“ˆ 21.68 Punkte

๐Ÿ“Œ Equifax's Data Breach By the Numbers: 146 Million Social Security Numbers, 99 Million Addresses, and More


๐Ÿ“ˆ 21.68 Punkte











matomo