Rounded number

Asked

Viewed 56 times

0

Speaks galeres to developing a control of funds at work, and when save a value it rounds. the real value is $ 678,74 and it rounds to 679 in the view. How can I solve this?

    'moeda_id' => function($value) {
    return ($obj = Moeda::findOne($value)) ? $obj->nome : $value;

Complete code:

```php
<?php

use app\models\Categoria;
use app\models\DespesaStatus;
use app\models\FonteRecursoCentroCusto;
use app\models\Moeda;
use app\models\Natureza;
use app\models\TipoDespesa;
use app\widgets\ChangeHistoryWidget;
use app\widgets\DetailView;
use wbraganca\enumerables\EnumBoolean;
use yii\helpers\Html;
use yii\helpers\Markdown;

/* @var $this yii\web\View */
/* @var $model app\models\Despesa */

$this->title = 'Despesa #' . $model->id;
$this->params['breadcrumbs'][] = ['label' => 'Despesas', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
\yii\web\YiiAsset::register($this);
?>
<div class="despesas-view tile">
    <?php if ($this->context->layout !== 'iframe'): ?>
    <div class="text-right page-actions">
        <?= Html::a('<i class="fa fa-list-ul" aria-hidden="true"></i> Listar', ['index'], ['class' => 'btn btn-primary']) ?>
        <?= Html::a('<i class="fa fa-plus" aria-hidden="true"></i> Adicionar', ['create'], ['class' => 'btn btn-info']) ?>
        <?= Html::a('<i class="fa fa-pencil" aria-hidden="true"></i> Alterar', ['update', 'id' => $model->id], ['class' => 'btn btn-warning']) ?>
        <?= Html::a('<i class="fa fa-trash" aria-hidden="true"></i> Excluir', ['delete', 'id' => $model->id], [
            'class' => 'btn btn-danger',
            'data' => [
                'confirm' => 'Tem certeza de que deseja excluir este item?',
                'method' => 'post',
            ],
        ]) ?>
    </div>
    <?php endif; ?>

    <?= DetailView::widget([
        'model' => $model,
        'attributes' => [
            'id',
            [
                'attribute' => 'natureza_id',
                'value' => $model->natureza->nome
            ],
            [
                'attribute' => 'categoria_id',
                'value' => $model->categoria->nome
            ],
            [
                'attribute' => 'tipo_despesa_id',
                'value' => $model->tipoDespesa->nome
            ],
            [
                'attribute' => 'fonte_recurso_centro_custo_id',
                'value' => $model->fonteRecursoCentroCusto->codigoDescricao
            ],
            'finalidade',
            'ano_competencia',
            'data_solicitacao:date',
            'enviado_rpcap:boolean',
            'motivo_rpcap',
            'numero_sharepoint',
            [
                'attribute' => 'moeda_id',
                'value' => $model->moeda->id . ' (' . $model->moeda->nome . ')'
            ],
            'valor_moeda_origem:decimal',
            'valor:currency',
            'data_inicio_realizacao_despesa:date',
            'data_fim_realizacao_despesa:date',
            [
                'attribute' => 'files',
                'value' => function($model) {
                    $links = [];

                    foreach ($model->anexos as $anexo) {
                        $links[] = Html::a(
                            $anexo->nome,
                            $anexo->base_url . DIRECTORY_SEPARATOR . $anexo->caminho,
                            ['target' => '_blank']
                        );
                    }

                    if (!empty($links)) {
                        return Html::ul($links, ['encode' => false]);
                    }
                },
                'format' => 'raw',
            ],
            [
                'attribute' => 'obs',
                'format' => 'html',
                'value' =>  Markdown::process($model->obs)
            ],
        ],
    ]) ?>

    <?= ChangeHistoryWidget::widget([
        'model' => $model,
        'customColumns' => [
            'enviado_rpcap' => function($value) {
                $options = EnumBoolean::getConstList();

                return isset($options[$value]) ? $options[$value] : $value;
            },
            'despesa_status_id' => function($value) {
                return ($obj = DespesaStatus::findOne($value)) ? $obj->nome : $value;
            },
            'natureza_id' => function($value) {
                return ($obj = Natureza::findOne($value)) ? $obj->nome : $value;
            },
            'categoria_id' => function($value) {
                return ($obj = Categoria::findOne($value)) ? $obj->nome : $value;
            },
            'tipo_despesa_id' => function($value) {
                return ($obj = TipoDespesa::findOne($value)) ? $obj->nome : $value;
            },
            'fonte_recurso_centro_custo_id' => function($value) {
                return ($obj = FonteRecursoCentroCusto::findOne($value)) ? $obj->codigoDescricao : $value;
            },
            'moeda_id' => function($value) {
                return ($obj = Moeda::findOne($value)) ? $obj->nome : $value;
            },
        ]
    ]) ?>
</div>
```
  • would help you? How to round up number?

  • post more code to understand what you need

  • Where you save the value?

1 answer

3

You can use the number_format for this operation, thus will appear the decimal places after the comma.

For example:

number_format ( float $number [, int $decimals ] ) : string

number_format ( float $number , int $decimals , string $dec_point , string $thousands_sep ) : string
  • Welcome Rennan, your answer may be useful, but exemplify it with a snippet of code. Also put the concept of number_format, and if you can find a bibliographic source, then it will make Jennifer’s doubts easier to resolve.

  • 1

    Hello Luiz, of course. I’ll put an example of number_format

  • Renan, thanks. but would you have any solution other than number_format? if not how would you implement it in my code? because I am a layman in PHP.

  • I’m also learning about language, but I’m researching to help you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.