0

I have a variable fl_duration which takes integer value 1,2,3. I am looking to create a new variable 'decimal' which will return the value as a decimal number i.e. 0.1 , 0.2, 0.3.

$decimal = get_field('fl_duration');

Any ideas would be much appreciated.

7
  • For 10+ values? 0.10 or 1.0 ?? Commented Nov 13, 2018 at 14:11
  • it's only 1,2,3 - no other values, so I shouldnt worry about that Commented Nov 13, 2018 at 14:13
  • What about dividing by ten? like function dec($flDur) {return $flDur/10;}; $decimal = dec($fl_duration); Commented Nov 13, 2018 at 14:16
  • 1
    Division by 10 ! Commented Nov 13, 2018 at 14:16
  • Or multiply by 0.1. Commented Nov 13, 2018 at 14:21

3 Answers 3

4

PHP will convert it to an float itself so you can just do it by basic math.

<?php

$int = 1;
$dec = $int / 10;
var_dump($int);
var_dump($dec);

Output:

int(1)

float(0.1)

Sign up to request clarification or add additional context in comments.

Comments

1

How about number_format(), is this what you are looking for? http://php.net/manual/en/function.number-format.php

$decimal = number_format( 1, 2); //will give you string(4) "1.00"

1 Comment

If you are unsure, you need to ask it in the comments section of the question.
1

You can also do it like this:

$int = 1;
$dec = (float) "0.$int";
// or
$dec = (double) "0.$int";
var_dump($dec);

Output:

float(0.1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.