KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

Formating a value #2415

Open Richb201 opened this topic on on Nov 4, 2021 - 7 comments

Richb201 commented on Nov 4, 2021

I am getting the total $ on the system with

    $sql=" SELECT SUM(credit) as total_credit FROM results ";
    $this->src('substantiator')
        ->query($sql)
        ->pipe($this->dataStore("total_credits"));

and then this in the view file:

echo $this->dataStore('total_credits')->sum("total_credit");

I'd like total_credit to appear as $ and with commas. for example $x,xxx,xxx. How can I do this? What is the largest amount that total_credit can be?

Sebastian Morales commented on Nov 4, 2021

Pls just use the php function number_format:

$total_credits = $this->dataStore('total_credits')->sum("total_credit");
$formatted_total_credits = number_format(
    $total_credits,
    $decimals, // default = 0
    $decimal_separator, // default = "."
    $thousands_separator // default = ","
);

The largest amount in PHP is PHP_FLOAT_MAX ~ 1.7976931348623E+308, that is 309 digits/zeros number with 14-digit preciseness.

Richb201 commented on Nov 4, 2021

I tried this but my IDE doesn't like the last line:

echo date('D M j Y');
echo (' total US tax credits documented by Research Study Online = ');
$total_$= $this->dataStore('total_credits')->sum("total_credit");
setlocale(LC_MONETARY, 'en_US');
echo money_format('%i', $total_$);

why? Can I not assign the value to $total_$?

KoolReport commented on Nov 4, 2021

When you run the code, is there any error showing?

Also if you want to use money_format(), please make sure your PHP is 7.4 or below since this function is deprecated in later version.

https://www.php.net/manual/en/function.money-format.php

BTW, Your IDE does not like the "_$" at the end. Just make a simple variable like this $usdTotal or simply $total

Hope that helps.

Richb201 commented on Nov 4, 2021

I am using php 7.2 and I have seen that link, That is where I got the above code from!

I got this working instead:



   $total_credit_amount = $this->dataStore('total_credits')->sum("total_credit");
    $formatted_total_credits = number_format( $total_credit_amount);
    echo "$".$formatted_total_credits;

Is there a way to modify the background of the echo to black and the char's to be white?

Sebastian Morales commented on Nov 5, 2021

You can wrap your text in a <div> or <span> tag and use css to make background black and foreground text white:

echo "<span style=' background-color: black; color: white; '>$text</span>" ;
Richb201 commented on Nov 5, 2021

Sebastian, it looks good except the black background is also scrolling. Anyway to just scroll the white text?

Sebastian Morales commented on Nov 8, 2021

Probably make your entire div row black instead of just the span tag.

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
help needed

None