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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:36:18+00:00 2026-05-27T03:36:18+00:00

I hold decimals in a database using DECIMAL(10,5) I would like to format these

  • 0

I hold decimals in a database using DECIMAL(10,5)

I would like to format these numbers according to a few rules:

  • A zero decimal should display as 0
  • Show a long decimal (no trailing zero’s) with all of it’s numbers
  • When possible, I would like to only show up to 2 decimal places (when there are trailing zeros)

Here are some examples:

  • The left side corresponds to how the number is stored in database.
  • The right number is how I would like to display the number in my application.

0.00000 => 0
0.51231 => 0.51231
0.12000 => 0.12
0.40000 => 0.40
0.67800 => 0.678
12.10000 => 12.10
  • 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-27T03:36:18+00:00Added an answer on May 27, 2026 at 3:36 am

    This will work for you:

    function format($x){
        if(!(int)substr_replace($x, '', $dpos = strpos($x, '.'), 1))
            return 0;
        else
            return str_pad((rtrim($x, '0')), $dpos + 3, '0'); 
    }
    

    Example

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

Sidebar

Related Questions

In PHP (using built-in functions) I'd like to convert/format a number with decimal, so
I want to store strings like this 1,5,6,7,9,45,20,45,78,81... Numbers can't be negative or decimal
I am using a mySql DECIMAL(12,4) column to hold price values (seeing how that's
I'm iterating through a range of cells which hold numbers with 2 decimal places.
It would hold a string and have characters (numeric, or decimal point) added and
I'd like to hold my components in a separate xml file for production, development
I have a database that hold's a user's optional profile. In the profile I
using SQL Server 2005 with SP4 and I am designing a database table. Here
basically i want to hold a parameter that retrieve value from $.post() call like
I am using just a simple DataGridView to hold a bunch of data (Funny

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.