Advertising sustains the DA. Ads are hidden for members. Join today

Computed Field

Converting Horsepower (float) into Kilowatts (float) and custom format it

Last updated on
30 April 2025

As i was building a client site, i had a request like this: when he creates a node (Vehicle content type), among all the fields, he fills in a float value for the horsepower of the engine. He wanted the system to automatically convert that value in Kilowatts to and display it along the horsepower value and add a suffix " KW".

After creating the computed field, this are my settings:

Computed Code (PHP)

$entity_field[0]['value'] =  $entity->field_mjet_kuajfuqi['und'][0]['value'] * 0.7456;

Where the field_mjet_kuajfuqi is the Horsepower field (i have named it in albanian) that i referenced to get the value, and multiplied it with the appropriate conversion value for 1 kilowatt.

Then on the Display Code (PHP) i did this:

$mycustomdisplay = number_format($entity_field_item['value'], 1, '.', '');
$display_output = $mycustomdisplay  . " KW";

I applied the number_format php function to force the output to have only 1 value after the decimal point. And after that i concatenated it with the suffix String namely " KW".

In the database storage settings i used:
Data type: float
Data size: normal
Decimal Precision: 10
Decimal Scale: 1 // which did not work and made me use the number format in the display code above.

Default value: - i forgot to put anything here and now i cant edit is since there is data into the database.

Hope this helps someone!

Elvin Xhimitiku
Custom Crafted Websites

Help improve this page

Page status: Not set

You can: