formatter->asLength how to change output measure name


(vkponomarev) #1

Hello

<?php $value = 10; echo Yii::$app->formatter->asLength($value); ?>

Output:
10 meters

How to change word meters to centimeters or inches or millimeter etc.
And so for the asWeigth.

Thank you.


(Bizley) #2

Hmm, looks like you would have to set

Yii::$app->formatter->baseUnits['length']['metric'] = 10;

before calling asLength() to change the unit to centimeters. I’m not sure why it’s done like that - maybe I’m wrong and there is fast way to change it. Check this piece of code for other possibilities.


(vkponomarev) #3

Thanks for answer.

Example numbers:

echo Yii::$app->formatter->asLength(0)
echo Yii::$app->formatter->asLength(0.5)
echo Yii::$app->formatter->asLength(1)
echo Yii::$app->formatter->asLength(1.6)
echo Yii::$app->formatter->asLength(4000)

  1. Yii::$app->formatter->baseUnits['length']['metric']

I don’t think that is exactly what i need.
Different numbers shows different measures names.
Still looking for answer.

1.0 Without this string

// Yii::$app->formatter->baseUnits['length']['metric'] = 1;

Output:

0 millimeters
50 centimeters
1 meter
1.6 meters
4 kilometers

1.1 Yii::$app->formatter->baseUnits['length']['metric'] = 1;

Output:

0 millimeters
500 meters
1 kilometer
1.6 kilometers
4000 kilometers

1.2 Yii::$app->formatter->baseUnits['length']['metric'] = 10;

Output:

0 millimeters
5 millimeters
1 centimeter
1.6 centimeters
40 meters

1.3 Yii::$app->formatter->baseUnits['length']['metric'] = 1000;

Output:

0 millimeters
50 centimeters
1 meter
1.6 meters
4 kilometers

1.4 Yii::$app->formatter->baseUnits['length']['metric'] = 1000000;

Output:

0 millimeters
500 meters
1 kilometer
1.6 kilometers
4000 kilometers

  1. asShortLength() and asLength() when used in the same page is getting style of first appears.

If sombody knows how to fix it pls tell me.

Yii::$app->formatter->baseUnits['length']['metric'] = 1000000;

echo Yii::$app->formatter->asShortLength(0)
echo Yii::$app->formatter->asShortLength(0.5)
echo Yii::$app->formatter->asShortLength(1)
echo Yii::$app->formatter->asShortLength(1.6)
echo Yii::$app->formatter->asShortLength(4000)

echo Yii::$app->formatter->asLength(0)
echo Yii::$app->formatter->asLength(0.5)
echo Yii::$app->formatter->asLength(1)
echo Yii::$app->formatter->asLength(1.6)
echo Yii::$app->formatter->asLength(4000)

Output:

0 mm
500 m
1 km
1.6 km
4000 km

0 mm
500 m
1 km
1.6 km
4000 km

  1. Yii::$app->formatter->baseUnits['length']['imperial'] = 792;

Not working still metric output

  1. Yii::$app->formatter->baseUnits['weight']['metric'] = 1000;

echo Yii::$app->formatter->asWeight(0)
echo Yii::$app->formatter->asWeight(0.5)
echo Yii::$app->formatter->asWeight(1)
echo Yii::$app->formatter->asWeight(1.6)
echo Yii::$app->formatter->asWeight(4000)

output
0 grams
500 grams
1 kilogram
1.6 kilograms
4 tons

Still looking for right usage of this formatters.

Thank you.