Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8485965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:48:07+00:00 2026-06-10T20:48:07+00:00

I am writing a program in C. Is there any way of determining the

  • 0

I am writing a program in C.

Is there any way of determining the number of decimal places
in a float number (i.e. 0.123 = 3, 0.123456 = 6 etc.)?

Other than converting the number to a string and using string
functions to pull the characters after the decimal place?

I have a function named computeFare()

float computeFare()
{
  totalFare =  (3.40 + basicFare) * (1 + surchargePercent);
  return totalFare; // **in this case totalFare = 34.567**
}

printf("Total fare is $%.2f\n", 
            computeFare());

Result : Total fare is $34.56

I did this and it returned 34.56…
I want it to be $34.57

Thanks,
Lawrence.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T20:48:09+00:00Added an answer on June 10, 2026 at 8:48 pm

    Not really, the number of decimal places is a function of the internal representation of the number. What you see when you display the number is, is the number as a string for printing.

    So if you are interested in the displayed value, you can control the number of decimal places with formatting directives in the first place e.g., %5.2f, or use the string function approach as you mention in your post after the fact once you have the number as a string.

    Also, as an aside, would you count trailing zeros?

    Perhaps it would be helpful to state your goal for wanting to do this? There might be other ways to accomplish what you are looking to do.

    Update:

    Based on your update, the problem is really not counting decimal places, rather you are running into represenation issue/rounding errors (not that uncommon). The problem is that some fractional values can’t be exactly represented in base 2. Here’s an exhaustive explanation: What Every Computer Scientist Should Know About Floating-Point Arithmetic.

    Take a look at this SO question: Understanding floating point representation errors; what's wrong with my thinking?

    You can try this approach to round the value:

    float rounded_val = floorf(value * 100.0 + 0.5) / 100.0; 
    

    Wikipeadia has a whole article on rounding.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way of writing pexpect like small program which can launch a
i'm writing a C# program, and i want to know if there is any
Is there any way of determining the current working directory of an already running
Is there any way to make a Java program (in Windows) that just acts
Is there any API for writing a C# program that could interface with Windows
Is there any way to end writing to the console, terminate the writing and
I am writing a C program and because there is no string in C,
I am writing a program that is cross platform. There are a few spots
I am newbie at using DataSet. I am writing a program with VB.NET, there
I'm writing a simple program. There is only one class in it. There is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.