SOLVED: PHP code to SUM other fields not adding the decimal places

5 months 2 weeks ago - 5 months 1 week ago #336151 by jcrimmel
I have a coding issue that ChatGPT has not been able to figure out for me, or maybe I'm not asking it the right questions. and was hoping some of the super geniuses here could help.

This is the PHP code for a Code Field named "cb_scope3totalpaymentamount". This field takes the SUM of 3 different Decimal Number fields. These 3 fields are set up as input with the Thousands Separator as a comma and the Decimal Separator as a period. The code works great... with the exception that it will NOT add the decimal places shown in the 3 fields it is adding. This particular example should return 1,704.28, but only shows 1,704.

I have checked that the other 3 fields are indeed Decimal Number fields, and they display the decimal places on screen. What am I missing??
$totalAmountForScope3 = $user->getInt("cb_scope3depositamount", 0)
    + $user->getInt("cb_scope3additionaldepositamount", 0)
    + $user->getInt("cb_scope3finalpaymentamount", 0);

// Format the total amount with two decimal places
$formattedTotalAmountForScope3 = number_format($totalAmountForScope3, 2, '.', ',');

return $formattedTotalAmountForScope3;

Please Log in to join the conversation.

5 months 2 weeks ago #336152 by jcrimmel
I have even tried FOLAT the Decimal Field Values before adding them and multiplying the result by 100, but it is still ignoring the decimal place values.

I'm 99.99% complete with building this site and this is the last thing to fix!
$depositAmount = (float) $user->getInt("cb_scope3depositamount", 0);
$additionalDepositAmount = (float) $user->getInt("cb_scope3additionaldepositamount", 0);
$finalPaymentAmount = (float) $user->getInt("cb_scope3finalpaymentamount", 0);

$totalAmountForScope3 = $depositAmount + $additionalDepositAmount + $finalPaymentAmount;

// Format the total amount with exactly two decimal places
$formattedTotalAmountForScope3 = number_format($totalAmountForScope3, 2, '.', '');

// Multiply the formatted total amount by 100
$resultAfterMultiplication = $formattedTotalAmountForScope3 * 100;

return $resultAfterMultiplication;

 

Please Log in to join the conversation.

5 months 2 weeks ago #336153 by jcrimmel
SO.... the below code prints some checks while it is running the script and tells me the "Orginal" value of the fields I'm trying to add do not have decimal places... even though they are Decimal Fields and display numbers after the decimal point.  
// Assuming $user is an object that has a method getInt()

// 1. Ensure the value is treated as a float
$cbScope3DepositAmount = (float) $user->getInt("cb_scope3depositamount", 0);

// 2. Debugging: Print values for inspection
echo "Original Value: $cbScope3DepositAmount<br>";

// 3. Ensure that locale settings do not interfere with decimal places
setlocale(LC_NUMERIC, 'en_US.UTF-8');

// Multiply the value by 100 before formatting
$valueBeforeFormatting = $cbScope3DepositAmount * 100;
echo "Value Before Formatting: $valueBeforeFormatting<br>";

// Format the value with exactly two decimal places
$formattedValue = number_format($valueBeforeFormatting, 2, '.', '');
echo "Formatted Value: $formattedValue<br>";

Please Log in to join the conversation.

5 months 2 weeks ago #336154 by jcrimmel
SOLVED:

Okay... so I'm not a great coder BUT I did realize I am using 'getInt' in all of this code. When I change it to 'get' it works perfectly.

Why I didn't think of this before is a rookie mistake. 

 

Please Log in to join the conversation.

5 months 2 weeks ago #336166 by krileon
For floats you can use getFloat. They're just statically typed GETTERS basically. Using just get is also fine and won't have a static type.


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.
The following user(s) said Thank You: jcrimmel

Please Log in to join the conversation.

Moderators: beatnantkrileon
Time to create page: 0.162 seconds

Facebook Twitter LinkedIn