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

  • Home
  • SEARCH
  • 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 865733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:41:06+00:00 2026-05-15T09:41:06+00:00

I would like to truncate the float to 4 digits. Are there some efficient

  • 0

I would like to truncate the float to 4 digits.
Are there some efficient way to do that?
My current solution is:

double roundDBL(double d,unsigned int p=4)
{
 unsigned int fac=pow(10,p);
 double facinv=1.0/static_cast<double>(fac);
 double x=static_cast<unsigned int>(d*fac)*facinv;
 return x;
}

but using pow and delete seems to me not so efficient.

kind regards

Arman.

  • 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-05-15T09:41:06+00:00Added an answer on May 15, 2026 at 9:41 am
    round(d*10000.0)/10000.0;
    

    or if p must be variable;

    double f = pow(10,p);
    round(d*f)/f;
    

    round will usually be compiled as a single instruction that is faster than converting to an integer and back. Profile to verify.

    Note that a double may not have an accurate representation to 4 decimal places. You will not truly be able to truncate an arbitrary double, just find the nearest approximation.

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

Sidebar

Related Questions

Is there a way in .NET to create a type derived from decimal that
I would like to state the row/col output names in a **ply function, ldply
I created a simple java function to truncate a string to be displayed in
I'm using a for-loop similar to this one to truncate all filenames in a
I'm afraid I couldn't find anything quite like this particular scenario. I have a
I'm using Flot to plot a line graph and my data set is a
I have the following dataset (all positive values) Input file 1 2.3456 1 5.02
I have the following function inside my HomeController class: public class HomeController : Controller
I have to create a number of new stores inside my Magento installation. One
Clarification: The creator of the post should be able to decide when the truncation

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.