Questions tagged [decimal]
Decimal is the name for our common base-ten numeral system. It may also refer to non-integer values expressed with a decimal point.
7,626
questions
2447
votes
18
answers
1.2m
views
Difference between decimal, float and double in .NET?
What is the difference between decimal, float and double in .NET?
When would someone use one of these?
1472
votes
39
answers
2.1m
views
How to round a number to n decimal places in Java
What I would like is a method to convert a double to a string which rounds using the half-up method - i.e. if the decimal to be rounded is 5, it always rounds up to the next number. This is the ...
1018
votes
7
answers
594k
views
decimal vs double! - Which one should I use and when? [duplicate]
I keep seeing people using doubles in C#. I know I read somewhere that doubles sometimes lose precision.
My question is when should a use a double and when should I use a decimal type?
Which type is ...
835
votes
19
answers
1.6m
views
How do I display a decimal value to 2 decimal places?
When displaying the value of a decimal currently with .ToString(), it's accurate to like 15 decimal places, and since I'm using it to represent dollars and cents, I only want the output to be 2 ...
807
votes
13
answers
77k
views
How to convert Decimal to Double in C#?
I want to assign the decimal variable "trans" to the double variable "this.Opacity".
decimal trans = trackBar1.Value / 5000;
this.Opacity = trans;
When I build the app it gives ...
498
votes
16
answers
886k
views
How do you round a number to two decimal places in C#?
I want to do this using the Math.Round function
458
votes
6
answers
233k
views
What does the M stand for in C# Decimal literal notation?
In order to work with decimal data types, I have to do this with variable initialization:
decimal aValue = 50.0M;
What does the M part stand for?
365
votes
32
answers
512k
views
Get decimal portion of a number with JavaScript
I have float numbers like 3.2 and 1.6.
I need to separate the number into the integer and decimal part. For example, a value of 3.2 would be split into two numbers, i.e. 3 and 0.2
Getting the integer ...
357
votes
14
answers
649k
views
How can I remove the decimal part from JavaScript number?
I have the results of a division and I wish to discard the decimal portion of the resultant number.
How can I do this?
355
votes
24
answers
390k
views
Python JSON serialize a Decimal object
I have a Decimal('3.9') as part of an object, and wish to encode this to a JSON string which should look like {'x': 3.9}. I don't care about precision on the client side, so a float is fine.
Is there ...
319
votes
3
answers
166k
views
Float vs Decimal in ActiveRecord
Sometimes, Activerecord data types confuse me. Err, often. One of my eternal questions is, for a given case,
Should I use :decimal or :float?
I've often come across this link, ActiveRecord: :...
310
votes
4
answers
398k
views
How do I interpret precision and scale of a number in a database?
I have the following column specified in a database: decimal(5,2)
How does one interpret this?
According to the properties on the column as viewed in SQL Server Management studio I can see that it ...
302
votes
11
answers
425k
views
Java String remove all non numeric characters but keep the decimal separator
Trying to remove all letters and characters that are not 0-9 and a period. I'm using Character.isDigit() but it also removes decimal, how can I also keep the decimal?
301
votes
12
answers
67k
views
When should I use double instead of decimal?
I can name three advantages to using double (or float) instead of decimal:
Uses less memory.
Faster because floating point math operations are natively supported by processors.
Can represent a larger ...
299
votes
8
answers
612k
views
How to store decimal values in SQL Server?
I'm trying to figure out decimal data type of a column in the SQL Server. I need to be able to store values like 15.5, 26.9, 24.7, 9.8, etc
I assigned decimal(18, 0) to the column data type but this ...
290
votes
19
answers
255k
views
Decimal precision and scale in EF Code First
I'm experimenting with this code-first approach, but I'm find out now that a property of type System.Decimal gets mapped to a sql column of type decimal(18, 0).
How do I set the precision of the ...
289
votes
9
answers
616k
views
Formatting a float to 2 decimal places
I am currently building a sales module for a clients website. So far I have got the sale price to calculate perfectly but where I have come stuck is formatting the output to 2 decimal places.
I am ...
270
votes
18
answers
1.1m
views
How do I restrict a float value to only two places after the decimal point in C?
How can I round a float value (such as 37.777779) to two decimal places (37.78) in C?
267
votes
13
answers
435k
views
How do I convert a decimal to an int in C#?
How do I convert a decimal to an int?
259
votes
7
answers
269k
views
Format decimal for percentage values?
What I want is something like this:
String.Format("Value: {0:%%}.", 0.8526)
Where %% is that format provider or whatever I am looking for.
Should result: Value: %85.26..
I basically need it for wpf ...
249
votes
23
answers
247k
views
Remove trailing zeros
I have some fields returned by a collection as
2.4200
2.0044
2.0000
I want results like
2.42
2.0044
2
I tried with String.Format, but it returns 2.0000 and setting it to N0 rounds the other values ...
233
votes
25
answers
380k
views
range() for floats
Is there a range() equivalent for floats in Python?
>>> range(0.5,5,1.5)
[0, 1, 2, 3, 4]
>>> range(0.5,5,0.5)
Traceback (most recent call last):
File "<pyshell#10>", line 1,...
217
votes
5
answers
203k
views
What are the parameters for the number Pipe - Angular 2
I have used the number pipe below to limit numbers to two decimal places.
{{ exampleNumber | number : '1.2-2' }}
I was wondering what the logic behind '1.2-2' was? I have played around with these ...
215
votes
28
answers
528k
views
Integer to hex string in C++
How do I convert an integer to a hex string in C++?
I can find some ways to do it, but they mostly seem targeted towards C. It doesn't seem there's a native way to do it in C++. It is a pretty simple ...
203
votes
6
answers
485k
views
Format number to 2 decimal places
I would like to know how can I output a number with 2 decimal places, without rounding the original number.
For example:
2229,999 -> 2229,99
I already tried:
FORMAT(2229.999, 2)
CONVERT(2229....
199
votes
13
answers
1.1m
views
Write a number with two decimal places SQL Server
How do you write a number with two decimal places for sql server?
196
votes
30
answers
176k
views
Remove useless zero digits from decimals in PHP
I'm trying to find a fast way to remove zero decimals from number values like this:
echo cleanNumber('125.00');
// 125
echo cleanNumber('966.70');
// 966.7
echo cleanNumber(844.011);
// 844.011
...
195
votes
8
answers
650k
views
Convert decimal to binary in python [duplicate]
Is there any module or function in python I can use to convert a decimal number to its binary equivalent?
I am able to convert binary to decimal using int('[binary_value]',2), so any way to do the ...
191
votes
8
answers
237k
views
Hexadecimal To Decimal in Shell Script
Can someone help me to convert a hexadecimal number to decimal number in a shell script?
E.g., I want to convert the hexadecimal number bfca3000 to decimal using a shell script. I basically want the ...
190
votes
3
answers
493k
views
How do I convert hex to decimal in Python? [duplicate]
I have some Perl code where the hex() function converts hex data to decimal. How can I do it on Python?
189
votes
39
answers
384k
views
How to get numbers after decimal point?
How do I get the numbers after a decimal point?
For example, if I have 5.55, how do i get .55?
186
votes
11
answers
328k
views
How can I format a String number to have commas and round?
What is the best way to format the following number that is given to me as a String?
String number = "1000500000.574" //assume my value will always be a String
I want this to be a String with the ...
175
votes
20
answers
447k
views
How to convert numbers between hexadecimal and decimal
How do you convert between hexadecimal numbers and decimal numbers in C#?
172
votes
2
answers
256k
views
Android - Round to 2 decimal places [duplicate]
Possible Duplicate:
Round a double to 2 significant figures after decimal point
I know that there are plenty of examples on how to round this kind numbers.
But could someone show me how to round ...
167
votes
9
answers
89k
views
Precise Financial Calculation in JavaScript. What Are the Gotchas?
In the interest of creating cross-platform code, I'd like to develop a simple financial application in JavaScript. The calculations required involve compound interest and relatively long decimal ...
155
votes
4
answers
271k
views
Limit to 2 decimal places with a simple pipe
I have found an example which limits a number to 2 decimal places AND turns the figure into a currency amount- eg £2.55.
{{ number | currency : 'GBP' : true : '1.2-2'}}
Is there a simple pipe which ...
145
votes
5
answers
227k
views
Cast a Double Variable to Decimal
How does one cast a double to decimal which is used when doing currency development. Where does the M go?
decimal dtot = (decimal)(doubleTotal);
139
votes
13
answers
598k
views
Double decimal formatting in Java
I'm having some problems formatting the decimals of a double. If I have a double value, e.g. 4.0, how do I format the decimals so that it's 4.00 instead?
137
votes
8
answers
394k
views
convert a JavaScript string variable to decimal/money
How can we convert a JavaScript string variable to decimal?
Is there a function such as:
parseInt(document.getElementById(amtid4).innerHTML)
134
votes
14
answers
126k
views
Best way to display decimal without trailing zeroes
Is there a display formatter that will output decimals as these string representations in c# without doing any rounding?
// decimal -> string
20 -> 20
20.00 -> 20
20.5 -> 20.5
20.5000 -&...
134
votes
17
answers
232k
views
Force decimal point instead of comma in HTML5 number input (client-side)
I have seen that some browsers localize the input type="number" notation of numbers.
So now, in fields where my application displays longitude and latitude coordinates, I get stuff like &...
124
votes
6
answers
162k
views
How to convert a decimal number into fraction?
I was wondering how to convert a decimal into a fraction in its lowest form in Python.
For example:
0.25 -> 1/4
0.5 -> 1/2
1.25 -> 5/4
3 -> 3/1
122
votes
20
answers
147k
views
Find number of decimal places in decimal value regardless of culture
I'm wondering if there is a concise and accurate way to pull out the number of decimal places in a decimal value (as an int) that will be safe to use across different culture info?
For example:
19.0 ...
117
votes
23
answers
365k
views
Remove trailing zeros from decimal in SQL Server
I have a column DECIMAL(9,6) i.e. it supports values like 999,123456.
But when I insert data like 123,4567 it becomes 123,456700
How to remove those zeros?
109
votes
32
answers
535k
views
C++ - Decimal to binary converting
I wrote a 'simple' (it took me 30 minutes) program that converts decimal number to binary. I am SURE that there's a lot simpler way so can you show me?
Here's the code:
#include <iostream>
#...
104
votes
8
answers
212k
views
Best way to get whole number part of a Decimal number
What is the best way to return the whole number part of a decimal (in c#)? (This has to work for very large numbers that may not fit into an int).
GetIntPart(343564564.4342) >> 343564564
...
99
votes
8
answers
336k
views
How to calculate difference in hours (decimal) between two dates in SQL Server?
I have to calculate the difference in hours (decimal type) between two dates in SQL Server 2008.
I couldn't find any useful technique to convert datetime to decimal with 'CONVERT' on MSDN.
Can ...
99
votes
16
answers
145k
views
Easy way of finding decimal places
Is there an easy way or integrated function to find out the decimal places of a floating point number?
The number is parsed from a string, so one way is to count the digits after the . sign, but that ...
98
votes
9
answers
106k
views
Moving decimal places over in a double
So I have a double set to equal 1234, I want to move a decimal place over to make it 12.34
So to do this I multiply .1 to 1234 two times, kinda like this
double x = 1234;
for(int i=1;i<=2;i++)
{
...
98
votes
3
answers
322k
views
Two decimal places using printf( )
I'm trying to write a number to two decimal places using printf() as follows:
#include <cstdio>
int main()
{
printf("When this number: %d is assigned to 2 dp, it will be: 2%f ", 94.9456, 94....