Butterbean Posted October 15, 2014 Share Posted October 15, 2014 (edited) Below is some PHP code that looks at a DB and outputs a value within the user declared time and date period, via user recall using a web app attached to it. The code is rounding the numbers but I want it to include 2 of the decimal places. The original numbers in the DB have up to 5 decimal digits. This code is making them whole numbers. i would like to include the first 2 digits. I suspect the float should somehow be float(2) . Not sure how to do that. thanks in advance <?php $sql = ";WITH TOTAL_FULL_ENERGY_SUM_BASE_WEEKDAY_SUMMER AS ( SELECT cdate, datepart(hh, cdate) as trans_hour, datepart(mi, cdate) as trans_minute, comm_id, max(cast(total_full_energy_b AS FLOAT)/1000) * meter_multiplier AS totalUsage, meter_multiplier FROM [radiogates].[dbo].[purge_data] LEFT OUTER JOIN [radiogates].[dbo].[ops_invoice] on [radiogates].[dbo].[purge_data].[comm_id] = [radiogates].[dbo].[ops_invoice].[meter_id] where comm_id='$comm_id' and meter_multiplier is not null group by comm_id, cdate, meter_multiplier ) SELECT top 1 *, datepart(weekday, cdate) as trans_date_day, datepart(month, cdate) as trans_date_month, datepart(hour, cdate) as trans_date_hour, DATEPART(minute, cdate) as trans_date_minute FROM TOTAL_FULL_ENERGY_SUM_BASE_WEEKDAY_SUMMER where datepart(weekday, cdate) IN ('2', '3', '4', '5', '6') AND DATEPART(MONTH, cdate) IN ('5','6','7','8','9','10') and cdate between '$base_date $base_start_time' and '$base_date $base_end_time' ORDER BY totalUsage desc"; $query = sqlsrv_query($conn, $sql);if ($query === false){ exit("<pre>".print_r(sqlsrv_errors(), true));}while ($row = sqlsrv_fetch_array($query)){ $sumUsageWKW += $row[totalUsage];}sqlsrv_free_stmt($query);?> Edited October 15, 2014 by Butterbean Quote Link to comment https://forums.phpfreaks.com/topic/291643-returning-2-digits-in-the-following-code/ Share on other sites More sharing options...
ginerjm Posted October 15, 2014 Share Posted October 15, 2014 Instead of wading thru your improperly posted code I'll just offer this up: number_format() Quote Link to comment https://forums.phpfreaks.com/topic/291643-returning-2-digits-in-the-following-code/#findComment-1493637 Share on other sites More sharing options...
Butterbean Posted October 15, 2014 Author Share Posted October 15, 2014 Sorry, I see now what I am supposed to do. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/291643-returning-2-digits-in-the-following-code/#findComment-1493638 Share on other sites More sharing options...
Butterbean Posted October 15, 2014 Author Share Posted October 15, 2014 Is it because I didn't include this below, before my code? error_reporting(E_ALL | E_NOTICE); ini_set(display_errors', "1"); Quote Link to comment https://forums.phpfreaks.com/topic/291643-returning-2-digits-in-the-following-code/#findComment-1493642 Share on other sites More sharing options...
Butterbean Posted October 15, 2014 Author Share Posted October 15, 2014 Well, I will try to fix my mistakes in posting on the next thread. For now, I have max(cast(total_full_energy_b AS FLOAT)/1000) * meter_multiplier AS totalUsage, meter_multiplier My problem is that I don't know how to manipulate FLOAT so that it gives me 2 decimals. Quote Link to comment https://forums.phpfreaks.com/topic/291643-returning-2-digits-in-the-following-code/#findComment-1493643 Share on other sites More sharing options...
ginerjm Posted October 15, 2014 Share Posted October 15, 2014 You didn't read my first post. Quote Link to comment https://forums.phpfreaks.com/topic/291643-returning-2-digits-in-the-following-code/#findComment-1493647 Share on other sites More sharing options...
Solution Barand Posted October 15, 2014 Solution Share Posted October 15, 2014 You can use ROUND() in the query SELECT ROUND(MAX(total_full_energy_b)/1000 * meter_multiplier, 2) AS totalUsage Quote Link to comment https://forums.phpfreaks.com/topic/291643-returning-2-digits-in-the-following-code/#findComment-1493649 Share on other sites More sharing options...
Butterbean Posted October 16, 2014 Author Share Posted October 16, 2014 Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/291643-returning-2-digits-in-the-following-code/#findComment-1493890 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.