## how to change the color of decimals points in php

currency format in php with examples

php move decimal point

php 2 decimal places without rounding

php number_format

sprintf php 2 decimal places

money_format not working in php

php decimal separator

how can I change the colors of ONLY decimals of a number in PHP? this is my function for formatting numbers

function formatNumber($input, $decimals = 'auto', $prefix = '', $suffix = '') { $input = floatval($input); $absInput = abs($input); if ($decimals === 'auto') { if ($absInput >= 0.01) { $decimals = 2; } elseif (0.0001 <= $absInput && $absInput < 0.01) { $decimals = 4; } elseif (0.000001 <= $absInput && $absInput < 0.0001) { $decimals = 6; } elseif ($absInput < 0.000001) { $decimals = 8; } } if($input>1000000000000000){ $result = ROUND(($input/1000000000000000),2).' TH '; }elseif($input>1000000000000){ $result = ROUND(($input/1000000000000),2).' T '; }elseif($input>1000000000){ $result = ROUND(($input/1000000000),2).' B '; }elseif($input>1000000) { $result = ROUND(($input / 1000000), 2) . ' M '; } else { $result = number_format($input, $decimals, config('decimal-separator','.'), config('thousand-separator', ',')) ; } return ($prefix ? $prefix : '') . $result. ($suffix ? $suffix : ''); }

and I use it like that

<?php echo formatNumber($chart['assist'], 2)?>

i want my decimals with a different color... can i use css there or add classes?

Here is an example of what I meant in my comment by manipulate the string:

<?php $n = 123.456; $whole = floor($n); // 123 $fraction = $n - $whole; // .456 //echo str_replace('.', '<span class="colorme">.</span>', $n); echo $whole . '<span class="colorme">.</span>' . substr($fraction, strpos($fraction, '.')+1);

//Simply do a string replace on the decimal point.

**UPDATED** break out parts, concatenate.

A client side approach with Javascript (with some jQuery) would be something like:

$('#myDiv').each(function () { $(this).html($(this).html().replace(/\./g, '<span class="colorme">.</span>')); //or decimal point and decimal number part... $(this).html($(this).html().replace(/\.([0-9]+)/g, '<span class="colorme">.$1</span>')); });

Remember that other locales don't always use `.`

for divider.

**PHP number_format() Function,** Specifies how many decimals. If this parameter is set, the number will be formatted with a dot (.) as decimal point. decimalpoint, Optional. Specifies what string to Specifies the number of decimal digits to round to. Default is 0: mode: Optional. Specifies a constant to specify the rounding mode: PHP_ROUND_HALF_UP - Default. Rounds number up to precision decimal, when it is half way there. Rounds 1.5 to 2 and -1.5 to -2; PHP_ROUND_HALF_DOWN - Round number down to precision decimal places, when it is half way there. Rounds 1.5 to 1 and -1.5 to -1

So with your existing code, you could do something like:

$dec_point = config('decimal-separator','.'); $wrapped_dec_point = "<span class='dec_point'>{$dec_point}</span>"; $result = number_format($input, $decimals, $wrapped_dec_point, config('thousand-separator', ',')) ;

and then of course, for your CSS, you would just need

.dec_point { color: magenta; }

**PHP money_format() Function,** If "+" is used, the local setting for + and - will be used (usually a sign in front of negative If x is 0, the decimal point and the digits to its right will not be shown. This will display exactly two digits after the decimal point. Advantage: If you want to display two digits after a float value only and not for int, then use this.

Here is shorter solution

$n = 123.456; $nums = explode(".",$n); echo $nums[0] . '<span class="colorme">.' . $nums[1] . '</span>';

**number_format - Manual,** The number being formatted. decimals. Sets the number of decimal points. dec_point You have to change the fractional digits with this function. But I want that Step 1: Find a number you can multiply by the bottom of the fraction to make it 10, or 100, or 1000, or any 1 followed by 0s. Step 2: Multiply both top and bottom by that number. Step 3. Then write down just the top number, putting the decimal point in the correct spot (one space from the right hand side for every zero in the bottom number)

**floor - Manual,** Change language: Warning: do not use Seppili's function to floor with decimals. mathematical functions lack a floating point version of the modulo operation, which returns the difference echo '<div style="background-color:#ffff99">'; The number to be formatted. If no other parameters are set, the number will be formatted without decimals and with comma (,) as the thousands separator. decimals: Optional. Specifies how many decimals. If this parameter is set, the number will be formatted with a dot (.) as decimal point: decimalpoint: Optional. Specifies what string to use for

**sprintf - Manual,** For s specifier: it acts as a cutoff point, setting a maximum character limit to the The precision specifier stands for the number of digits after the decimal point Suppose the values are 1,2,1,3, all less than 5 then chart will show these values in y-axis with decimal breakup. If I change min value to 0 and max value to 5, data will come as expected (In complete number) but this can't be done becoz I can't predict max number. Please find attahced the sample bar graph.

**floatval - Manual,** Change language: of a variable; strval() - Get string value of a variable; settype() - Set the type of a variable; Type juggling $str); // replace dots (thousand seps) with blancs More elegant function with selection of decimal point (deafault ,): Method 1: Using number_format() function. Using number_format(), you can format a number. The first parameter of the function is the number to be formatted. The second parameter formats the first parameter with the number of decimals it shows and add a dot in front of it.

##### Comments

- CSS can't do that but you could use javascript client side to accomplish it. Alternately you need to manipulate that string output in PHP and add an HTML tag like a
`<span>`

. - i suggest use a custom CSS for change this
- please check this for php method barelyfitz.com/projects/csscolor
- @Tegito123 CSS is not able to work on specific characters. It works on elements in the DOM. You can't select the
`.`

(periods) in a blurb of text. - Just seconding that string replace to wrap with a span and class works very well.
- my bad bro... i want decimals numbers i mean 123.456 (.456 will be in different color) and the numbers is not stable it will change
- Ah man! You made me copy and paste two lines :) Now you know two approaches.
- yo! in your javascript you didn't consider I want the numbers after . would be colored too how can I do that?
- i've used your js like this
`$('.price').each(function () { $(this).html($(this).html().replace(/\./g, '<span class="decimals">.</span>')); });`

but i want a range of number from 0 to 999 would be colored too