๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ g-fact-41-setting-decimal-precision-in-c
Setting decimal precision in C - GeeksforGeeks
January 10, 2025 - For example, 5.48958123 should ... { float num = 5.48958123; // 4 digits after the decimal point num = floor(10000*num)/10000; printf("%f", num); return 0; }...
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ c_data_types_dec.php
C Data Types Decimal Precision
You have probably already noticed ... Yourself ยป ยท If you want to remove the extra zeros (set decimal precision), you can use a dot (.) followed by a number that specifies how many digits that should be shown after the decimal ...
Discussions

Is it possible to count the number of digits after decimal point in C? - Stack Overflow
Tell me if it's anyhow possible to find out the number of digits after the decimal point in C. ... It isn't possible to do it reliably unless the fractional part ends with a 5, and even that's subject to caveats. ... The problem is that 2.34 can't be represented exactly in binary floating point. See stackoverflow.com/questions/588004/โ€ฆ ... It depends. If you format it with %.3f you get ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Digits after decimal place. - C++ Forum
Define the parameter as a "double" then inside the function convert the number to a string. using "string.find" get the position of the "." then you can use simple math to figure the amount of digits to the left and right of the ".". Just a thought and I will have to test it later. Andy ... I wouldn't bother counting post-decimal... More on cplusplus.com
๐ŸŒ cplusplus.com
C/C++ counting the number of decimals? - Stack Overflow
If (case A) you can avoid converting ... the digits after the decimal point and subtract the number of trailing zeros. If you cannot do it (case B), then you need to make an assumption about the maximum number of decimals, convert the number back into string representation and round it to this maximum number using the round-to-even method. For example, if the user supplies 1.1 which gets represented ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to extract the decimal part from a floating point number in C? - Stack Overflow
How can we extract the decimal part of a floating point number and store the decimal part and the integer part into two separate integer variables? More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Cprogramming
cboard.cprogramming.com โ€บ c-programming โ€บ 138115-how-get-value-before-after-decimal-point-c.html
How to get a value Before/After a decimal point in C ??
May 18, 2011 - You can use the trunc function in math.h to get the integer portion, then subtract that from the float to get the decimal portion.
๐ŸŒ
Quora
quora.com โ€บ How-do-I-find-a-specific-digit-after-a-decimal-in-C
How to find a specific digit after a decimal in C - Quora
Answer: Itโ€™s very simple: Let me explain the logic: Say the number is 72.1454 which you mentioned. Now if you want 1st digit after decimal, then multiply by 10, and for 2nd digit multiply by 100 and so onโ€ฆFor 1st digit we get 721.454 after ...
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 3108546 โ€บ how-to-f-to-output-the-desired-digit-of-decimal-floatsaka-after-point
How to %f to output the desired digit of decimal & floats(aka after point) | Sololearn: Learn to code for FREE!
November 30, 2022 - In `printf("%x.yf", someFloat);` the x represent the total digits to be printed, it will self-adjust to the length of the integer part if it is shorter or left out. y is the digits after decimal point to be printed.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 61927111 โ€บ is-it-possible-to-count-the-number-of-digits-after-decimal-point-in-c
Is it possible to count the number of digits after decimal point in C? - Stack Overflow
Tell me if it's anyhow possible to find out the number of digits after the decimal point in C. ... It isn't possible to do it reliably unless the fractional part ends with a 5, and even that's subject to caveats. ... The problem is that 2.34 can't be represented exactly in binary floating point. See stackoverflow.com/questions/588004/โ€ฆ ... It depends. If you format it with %.3f you get ...
๐ŸŒ
Cplusplus
cplusplus.com โ€บ forum โ€บ beginner โ€บ 276317
Digits after decimal place. - C++ Forum
Define the parameter as a "double" then inside the function convert the number to a string. using "string.find" get the position of the "." then you can use simple math to figure the amount of digits to the left and right of the ".". Just a thought and I will have to test it later.
Top answer
1 of 16
22

Two ways I know of, neither very clever unfortunately but this is more a limitation of the environment rather than me :-)

The first is to sprintf the number to a big buffer with a "%.50f" format string, strip off the trailing zeros then count the characters after the decimal point. This will be limited by the printf family itself. Or you could use the string as input by the user (rather than sprintfing a floating point value), so as to avoid floating point problems altogether.

The second is to subtract the integer portion then iteratively multiply by 10 and again subtract the integer portion until you get zero. This is limited by the limits of computer representation of floating point numbers - at each stage you may get the problem of a number that cannot be represented exactly (so .2155 may actually be .215499999998). Something like the following (untested, except in my head, which is about on par with a COMX-35):

count = 0
num = abs(num)
num = num - int(num)
while num != 0:
    num = num * 10
    count = count + 1
    num = num - int(num)

If you know the sort of numbers you'll get (e.g., they'll all be 0 to 4 digits after the decimal point), you can use standard floating point "tricks" to do it properly. For example, instead of:

while num != 0:

use

while abs(num) >= 0.0000001:
2 of 16
9

Once the number is converted from the user representation (string, OCR-ed gif file, whatever) into a floating point number, you are not dealing with the same number necessarily. So the strict, not very useful answer is "No".

If (case A) you can avoid converting the number from the string representation, the problem becomes much easier, you only need to count the digits after the decimal point and subtract the number of trailing zeros.

If you cannot do it (case B), then you need to make an assumption about the maximum number of decimals, convert the number back into string representation and round it to this maximum number using the round-to-even method. For example, if the user supplies 1.1 which gets represented as 1.09999999999999 (hypothetically), converting it back to string yields, guess what, "1.09999999999999". Rounding this number to, say, four decimal points gives you "1.1000". Now it's back to case A.

๐ŸŒ
IncludeHelp
includehelp.com โ€บ c-programs โ€บ input-float-value-and-print-it-with-specified-digit-after-decimal-point.aspx
Input float value and print it with specified digit after decimal point in C
March 10, 2024 - Input a float value and we have to print the input value by specifying/setting different decimal precision in C. Input: Enter float value: 12.34567 Output: 12 12.3 12.35 12.3457 ยท To set decimal precision, we use the following format specifier with the printf() statement, ... # include <stdio.h> int main () { float value; printf("Enter float value: "); scanf("%f", &value); //print without decimal point printf("%0.0f\n", value); //print 1 digits after decimal point printf("%0.1f\n", value) ; //print 2 digits after decimal point printf("%0.2f\n", value) ; //print 4 digits after decimal point printf("%0.4f\n", value); return 0; }
Find elsewhere
๐ŸŒ
Medium
medium.com โ€บ @brajagopal.tripathi โ€บ how-do-you-get-the-number-after-the-decimal-point-in-c-cc7059eea231
How do you get the number after the decimal point in C++? | by Brajagopal Tripathi | Medium
August 31, 2023 - Use the %f format specifier in the printf() function. This will print the number with a decimal point and two digits after the decimal point. For example, the following code will print the number 12.345 as 12.35: ... Use the std::stod() function.
๐ŸŒ
OpenGenus
iq.opengenus.org โ€บ return-two-digit-after-decimal-point
Return two digit after decimal point in C
January 6, 2023 - #include<stdio.h> #include<stdlib.h> int digit(double a) { return (int)((int)(a*100.0))0; } int main(int argc,char** argv) { double input=atof(argv[1]); int answer=digit(input); printf("The two digits ater decimal point are %d",answer); return 1; } ... With this article at OpenGenus, you must have the complete idea of how to return two digit after decimal point in C.
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 108179 โ€บ how-to-count-the-number-of-digits-after-a-decimal-point
How to count the number of digits after a decimal point? | Sololearn: Learn to code for FREE!
So if you want to make the program handle large number of decimals than use string for input and simply count all the characters after '.' char a[50]; int i=0, L; scanf("%s",a); L= strlen(a); while(a[i]!='.') { i++; } printf("%d",L-i-1); use ...
๐ŸŒ
Quora
quora.com โ€บ How-do-you-get-the-number-after-the-decimal-point-in-C
How to get the number after the decimal point in C++ - Quora
Answer (1 of 4): https://www.geeksforgeeks.org/ Rounding Floating Point Number To two Decimal Places in C and C++ How to round off a floating point value to two places. For example, 5.567 should become 5.57 and 5.534 should become 5.53 First Method:- Using Float precision Take a step-up from t...
๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ maximum decimal places
r/C_Programming on Reddit: Maximum decimal places
June 10, 2020 -

What is the maximum decimal places that can be stored using c? I tried to store - 1.86264514923107200935514487085e-09 but only got 0.00000000186264514923107200000000.....

Top answer
1 of 5
3
https://en.wikipedia.org/wiki/Double-precision_floating-point_format Significant precision is 53 bits = 9.00719925e15, so that's 15 (almost 16) decimal digits of significance
2 of 5
3
If you care about preserving the value across a complete decimal โ†’ floating-point โ†’ decimal round-trip: #include #include int main(void) { printf("FLT_DIG = %d\n", FLT_DIG); printf("DBL_DIG = %d\n", DBL_DIG); printf("LDBL_DIG = %d\n", LDBL_DIG); } On my system, I get: FLT_DIG = 6 DBL_DIG = 15 LDBL_DIG = 18 Note that this does not mean that all values with at most this many decimal digits can be represented "exactly". It just means that the round-trip will yield the same decimal digits at the end. Note also that "decimal digits" here is not just the digits after the decimal point. 1234.56 has 6 significant decimal digits. If stored in a float and then reformatted as a decimal value, it will yield the same decimal value on my system. But 1234.567 has 7 significant decimal digits, and this is not guaranteed to yield the same decimal value if round-tripped through a float on my system. If you care about a floating-point โ†’ decimal โ†’ floating-point round-trip, you need more decimal digits: #include #include int main(void) { printf("FLT_DECIMAL_DIG = %d\n", FLT_DECIMAL_DIG); printf("DBL_DECIMAL_DIG = %d\n", DBL_DECIMAL_DIG); printf("LDBL_DECIMAL_DIG = %d\n", LDBL_DECIMAL_DIG); } I get: FLT_DECIMAL_DIG = 9 DBL_DECIMAL_DIG = 17 LDBL_DECIMAL_DIG = 21 These values are greater than the ones before because round-tripping in this direction needs to preserve the precision in the original floating-point value.
Top answer
1 of 8
6

The problem isn't really solvable as stated, since floating-point is typically represented in binary, not in decimal. As you say, many (in fact most) decimal numbers are not exactly representable in floating-point.

On the other hand, all numbers that are exactly representable in binary floating-point are decimals with a finite number of digits -- but that's not particularly useful if you want a result of 2 for 3.44.

When I run your code snippet, it says that 3.44 has 2 digits after the decimal point -- because 3.44 * 10.0 * 10.0 just happens to yield exactly 344.0. That might not happen for another number like, say, 3.43 (I haven't tried it).

When I try it with 1.0/3.0, it goes into an infinite loop. Adding some printfs shows that no becomes exactly 33333333333333324.0 after 17 iterations -- but that number is too big to be represented as an int (at least on my system), and converting it to int has undefined behavior.

And for large numbers, repeatedly multiplying by 10 will inevitably give you a floating-point overflow. There are ways to avoid that, but they don't solve the other problems.

If you store the value 3.44 in a double object, the actual value stored (at least on my system) is exactly 3.439999999999999946709294817992486059665679931640625, which has 51 decimal digits in its fractional part. Suppose you really want to compute the number of decimal digits after the point in 3.439999999999999946709294817992486059665679931640625. Since 3.44 and 3.439999999999999946709294817992486059665679931640625 are effectively the same number, there's no way for any C function to distinguish between them and know whether it should return 2 or 51 (or 50 if you meant 3.43999999999999994670929481799248605966567993164062, or ...).

You could probably detect that the stored value is "close enough" to 3.44, but that makes it a much more complex problem -- and it loses the ability to determine the number of decimal digits in the fractional part of 3.439999999999999946709294817992486059665679931640625.

The question is meaningful only if the number you're given is stored in some format that can actually represent decimal fractions (such as a string), or if you add some complex requirement for determining which decimal fraction a given binary approximation is meant to represent.

There's probably a reasonable way to do the latter by looking for the unique decimal fraction whose nearest approximation in the given floating-point type is the given binary floating-point number.

2 of 8
6

I doubt this is what you want since the question is asking for something that's not usually meaningful with floating point numbers, but here is the answer:

int digits_after_decimal_point(double x)
{
    int i;
    for (i=0; x!=rint(x); x+=x, i++);
    return i;
}
๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ decimal places
r/C_Programming on Reddit: Decimal places
September 15, 2018 -

Can I create a variable and set it to a specific decimal place? For example "float z to 5 dp" where z would be recorded to 5 dp for the rest of the program

๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ c_data_types_numbers.php
C Numeric Data Types
Use int when you need to store a whole number without decimals, like 35 or 1000, and float or double when you need a floating point number (with decimals), like 9.99 or 3.14515. int myNum = 1000; printf("%d", myNum); Try it Yourself ยป ยท float ...