Skip to content
Fix Code Error

Difference between decimal, float and double in .NET?

March 13, 2021 by Code Error
Posted By: Tom

What is the difference between decimal, float and double in .NET?

When would someone use one of these?

Solution

float and double are floating binary point types. In other words, they represent a number like this:

10001.10010110011

The binary number and the location of the binary point are both encoded within the value.

decimal is a floating decimal point type. In other words, they represent a number like this:

12345.65789

Again, the number and the location of the decimal point are both encoded within the value – that’s what makes decimal still a floating point type instead of a fixed point type.

The important thing to note is that humans are used to representing non-integers in a decimal form, and expect exact results in decimal representations; not all decimal numbers are exactly representable in binary floating point – 0.1, for example – so if you use a binary floating point value you’ll actually get an approximation to 0.1. You’ll still get approximations when using a floating decimal point as well – the result of dividing 1 by 3 can’t be exactly represented, for example.

As for what to use when:

  • For values which are “naturally exact decimals” it’s good to use decimal. This is usually suitable for any concepts invented by humans: financial values are the most obvious example, but there are others too. Consider the score given to divers or ice skaters, for example.

  • For values which are more artefacts of nature which can’t really be measured exactly anyway, float/double are more appropriate. For example, scientific data would usually be represented in this form. Here, the original values won’t be “decimally accurate” to start with, so it’s not important for the expected results to maintain the “decimal accuracy”. Floating binary point types are much faster to work with than decimals.

Answered By: Jon Skeet

Related Articles

  • Trouble using ROW_NUMBER() OVER (PARTITION BY ...)
  • Most effective way to parse JSON Objects
  • C++ template,typename and operator
  • Why doesn't the height of a container element…
  • I do not use TBB, but I get linker errors related to TBB
  • Good way of getting the user's location in Android
  • What's the difference between a single precision and…
  • What are the undocumented features and limitations…
  • How do SO_REUSEADDR and SO_REUSEPORT differ?
  • Python: Float to Decimal conversion and subsequent…
  • How to truncate float values?
  • Reference — What does this symbol mean in PHP?
  • powershell regex bug
  • how to check a pyspark dataframe value against…
  • R - Using loops to search one variable with another…
  • Ukkonen's suffix tree algorithm in plain English
  • How can I determine whether a 2D Point is within a Polygon?
  • Use of PUT vs PATCH methods in REST API real life scenarios
  • Is floating point math broken?
  • Rounding Bigdecimal values with 2 Decimal Places
  • Design DFA accepting binary strings divisible by a…
  • Printf width specifier to maintain precision of…
  • Difference between numeric, float and decimal in SQL Server
  • How would I be able to multiple select and pass data…
  • Convert all value in a array of object to string?
  • Retain precision with double in Java
  • binding backbone form view UIto model change to…
  • Draw in Canvas by finger, Android
  • Memcached vs. Redis?
  • Why does a fixed-point scaling factor tend to be a…
  • Sorting 1 million 8-decimal-digit numbers with 1 MB of RAM
  • What is the difference between float and double?
  • What is an optional value in Swift?
  • C: convert double to float, preserving decimal point…
  • Ubuntu apt-get unable to fetch packages
  • Smart way to truncate long strings
  • How can I format a decimal to always show 2 decimal places?
  • "Thinking in AngularJS" if I have a jQuery background?
  • 'foo' was not declared in this scope c++
  • What represents a double in sql server?
  • How to implement the factory method pattern in C++ correctly
  • data.table vs dplyr: can one do something well the…
  • Getting the closest string match
  • Addition of two queries - SQL/HANA
  • SQL query return data from multiple tables
  • What is your most productive shortcut with Vim?
  • Import Python Script Into Another?
  • Pandas - Reshape a dataframe columns based on…
  • Why does C++ code for testing the Collatz conjecture…
  • What is a NullReferenceException, and how do I fix it?
  • Why not use Double or Float to represent currency?
  • The definitive guide to form-based website authentication
  • How does PHP 'foreach' actually work?
  • What's the difference between Instant and LocalDateTime?
  • I have just started learning function pointers in C…
  • Why are elementwise additions much faster in…
  • How do the PHP equality (== double equals) and…
  • Getting started with Haskell
  • How do I rotate text in css?
  • Is there a more efficient way to divide and conquer…
  • How do you round to 1 decimal place in Javascript?
  • Is CSS Turing complete?
  • Calculating distance between two geographic locations
  • Start redis-server with config file
  • What's the best way of scraping data from a website?
  • C++ OpenGL stb_image.h errors
  • What does "dereferencing" a pointer mean?
  • What's the difference between eval, exec, and compile?
  • What are type hints in Python 3.5?
  • Android Image View Pinch Zooming
  • How to sum multiple different objects in an array…
  • Managing longs and short orders like LucF PineCoders…
  • Form field border-radius is not working only on the…
  • What is an application binary interface (ABI)?
  • Float vs Decimal in ActiveRecord
  • What do these operators mean (** , ^ , %, //)?
  • What are the real-world strengths and weaknesses of…
  • How to generate a random string of a fixed length in Go?
  • Questions every good .NET developer should be able…
  • SPA best practices for authentication and session management
  • Logging best practices
  • Nodejs convert string into UTF-8
  • What does "Fatal error: Unexpectedly found nil while…
  • Backbone.js - Should nested Views maintain…
  • What does the CSS rule "clear: both" do?
  • Why does changing 0.1f to 0 slow down performance by 10x?
  • What is the copy-and-swap idiom?
  • min and max value of data type in C
  • How should a model be structured in MVC?
  • How to implement a basic iterative pushdown…
  • Limit file format when using ?
  • How to get UTF-8 working in Java webapps?
  • collision detection from picturebox in a list c#
  • Convert float to double without losing precision
  • What does this symbol mean in JavaScript?
  • How can I find the product GUID of an installed MSI setup?
  • Random normal distribution generator comparison
  • How do I extract data from JSON with PHP?
  • Clang vs GCC - which produces faster binaries?
  • ExpressJS How to structure an application?

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

Terminating a script in PowerShell

Next Post:

Difference between == and === in JavaScript

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error