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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:18:21+00:00 2026-06-10T05:18:21+00:00

I need to do this rather strange thing, let’s say i have: $number =

  • 0

I need to do this rather strange thing, let’s say i have:

$number = rand(1, 9);

(This is just an example of what number it could be, in reality i get it in entirely different way)

And now i need “convert” that number to 0.2 or whatever number i got, basically it has to begin with 0 and be a float type of number.

  • 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-10T05:18:22+00:00Added an answer on June 10, 2026 at 5:18 am

    PHP does not support explicit type casting in variable declaration. To convert the int to a float in the way you want to simply divide by 10:

    $number = rand(1, 9) / 10;
    

    See this page on PHP Type Juggling for more info. If you mix floats and ints or other types they will be re-casted. Exmple:

    echo 10 + 2.5; // gives you 12.5, a float because of the types used
    

    Edit: PHP does have explicit type casting, just not in variable declaration. But even if you cast an integer as a float, it won’t display with a decimal place. To do that use PHP’s number_format function instead:

    echo number_format(10, 1); // gives you 10.0
    

    Edit 2: If you simply want to make your number a decimal between 0 and 1 (such that 2 becomes 0.2, 25 becomes 0.25, etc.) you could use the following function:

    function getNumAsDecimal($num) {
        return ($num / pow(10, strlen((string)$num)));
    }
    

    So getNumAsDecimal(2) would return 0.2.

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

Sidebar

Related Questions

Let's say that I have a form which runs a stored procedure. This stored
I know this may seem like a strange question, but say I have two
I'm asking this out of curiosity rather than due to a real need to
I am creating a responsive website, and have just noticed a strange behaviour in
Ok, I have a rather strange request. Due to the inconsistency in how different
I have an Entity. And sometimes I need this object also contains some value,
I have this rather simple SQL query, but it takes almost a minute to
Let's say you have the following HTML: <html> <div style=text-align: center;> <img src=http://www.ps.uci.edu/~tomba/ants/sugar.jpg width=50
I have run into a strange situation where I need to float two elements.
I have noticed a rather strange behaviour in IE. I have a HTML form

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.