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 7717345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:52:26+00:00 2026-06-01T02:52:26+00:00

i have value in php variable like that $var=’2.500000550′; echo $var what i want

  • 0

i have value in php variable like that

$var='2.500000550';
echo $var

what i want is to delete all decimal points after 2 digits.

like now value of variable will be

$var='2.50';
echo $var

keep in mind this value is coming from mysql databse

but when i use round php function i got round but i dont need round, i just need to delete all digits after 2 decimal simple.

i have tired, flot() and lot of other option no success.

Thanks

  • 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-01T02:52:28+00:00Added an answer on June 1, 2026 at 2:52 am

    TL;DR:

    The PHP native function bcdiv seems to do precisely what is required, and properly.

    To simply “truncate” a number, bcdiv($var, 1, 2); where 2 is the number of decimals to preserve (and 1 is the denomenator – dividing the number by 1 allows you to simply truncate the original number to the desired decimal places)

    Full Answer (for history)

    This turns out to be more elusive than one might think.

    After this answer was (incorrectly) upvoted quite a bit, it has come to my attention that even sprintf will round.

    Rather than delete this answer, I’m turning it into a more robust explanation / discussion of each proposed solution.

    number_format – Incorrect. (rounds)
    Try using number format:

    $var = number_format($var, 2, '.', '');  // Last two parameters are optional
    echo $var;
    // Outputs 2.50
    

    If you want it to be a number, then simply type-cast to a float:

    $var = (float)number_format($var, 2, '.', '');
    

    Note: as has been pointed out in the comments, this does in fact round the number.

    sprintf – incorrect. (sprintf also rounds)
    If not rounding the number is important, then per the answer below, use sprintf:

    $var = sprintf("%01.2f", $var);
    

    floor – not quite! (floor rounds negative numbers)

    floor, with some math, will come close to doing what you want:

    floor(2.56789 * 100) / 100; // 2.56
    

    Where 100 represents the precision you want. If you wanted it to three digits, then:

    floor(2.56789 * 1000) / 1000; // 2.567
    

    However, this has a problem with negative numbers. Negative numbers still get rounded, rather than truncated:

    floor(-2.56789 * 100) / 100; // -2.57
    

    “Old” Correct answer: function utilizing floor

    So a fully robust solution requires a function:

    function truncate_number( $number, $precision = 2) {
        // Zero causes issues, and no need to truncate
        if ( 0 == (int)$number ) {
            return $number;
        }
        // Are we negative?
        $negative = $number / abs($number);
        // Cast the number to a positive to solve rounding
        $number = abs($number);
        // Calculate precision number for dividing / multiplying
        $precision = pow(10, $precision);
        // Run the math, re-applying the negative value to ensure returns correctly negative / positive
        return floor( $number * $precision ) / $precision * $negative;
    }
    

    Results from the above function:

    echo truncate_number(2.56789, 1); // 2.5
    echo truncate_number(2.56789);    // 2.56
    echo truncate_number(2.56789, 3); // 2.567
    
    echo truncate_number(-2.56789, 1); // -2.5
    echo truncate_number(-2.56789);    // -2.56
    echo truncate_number(-2.56789, 3); // -2.567
    

    New Correct Answer

    Use the PHP native function bcdiv

    echo bcdiv(2.56789, 1, 1);  // 2.5
    echo bcdiv(2.56789, 1, 2);  // 2.56
    echo bcdiv(2.56789, 1, 3);  // 2.567
    echo bcdiv(-2.56789, 1, 1); // -2.5
    echo bcdiv(-2.56789, 1, 2); // -2.56
    echo bcdiv(-2.56789, 1, 3); // -2.567
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file that defines constant variables, like this: define_vars.php <? define(something,value); define(something1,value);
I have a JavaScript variable that I echo out using PHP which is shown
Assume that I have a class value object defined in php, where each variable
basically i want to hold a parameter that retrieve value from $.post() call like
I have an website with URL`s like example.com/index.php?p=home An this script $(document).ready(function() { var
I have a div like this : <td id=length data-length=<?php echo $length;?> > And
Say I have an array of key/value pairs in PHP: array( 'foo' => 'bar',
I have a PHP 2D array, many keys, each with one value, I need
Let's say I have this url: www.example.com/test.php?page=4 How do i retrieve the value of
I have a php loop that is echoing out geolocation values. How can I

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.