3

I have a VF Page with apex tags and have to transform it in HTML to adapt for Salesforce1. With apex I was able to format a text field as number and get only 2 decimals:

<apex:pageBlockTable value="{!LBo}" var="o" style="width:300px" >
    <apex:column headerValue="My Annual Leave Balance : ">
        <apex:outputText value="{0, number, #0.00}">           
            <apex:param value="{!VALUE(o.Units__c)}"/>
        </apex:outputText>
    </apex:column>
    <apex:column headerValue="No of Days : ">
        <apex:outputText value="{0, number, #0.00}">
            <apex:param value="{!VALUE(o.Units__c)/7.5}"/>
        </apex:outputText>
    </apex:column>           
</apex:pageBlockTable>

How can I get the same formating in HTML?

<table class="table" value="{!LBo}" var="o">
    <thead>
        <tr>
            <th>My Annual Leave Balance</th>
            <th>No of Days</th>
        </tr>
    </thead>
    <tbody>
        <apex:repeat value="{!LBo}" var="o">
            <tr> 
                <td>{!VALUE(o.Units__c}</td>
                <td>{!VALUE(o.Units__c)/7.5}</td>                        
            </tr>
        </apex:repeat>
    </tbody>
</table> 
1
  • 1
    Does ROUND(number, num_digits) not suit your needs?
    – Adrian Larson
    Sep 28 '16 at 2:36
3

I recommend using the ROUND(number, num_digits) function. From the Visualforce Developer Guide on Functions:

Function
ROUND

Description
Returns the nearest number to a number you specify, constraining the new number by a specified number of digits.

Use
ROUND(number, num_digits) and replace number with the field or expression you want rounded; replace num_digits with the number of decimal places you want to consider when rounding.

In this case, you need:

<td>{!ROUND(VALUE(o.Units__c), 2)}</td>
<td>{!ROUND(VALUE(o.Units__c)/7.5, 2)}</td> 
0
0

I would think of writing this kind of logic upon injecting javacript:

<apex:repeat value="{!LBo}" var="o">
<tr> 
<td>{!VALUE(o.Units__c}</td>                
<script>
    var num = '{!VALUE(o.Units__c)/7.5}';
    var n = num.toFixed(2);
    document.write("<td>" + n + "</td>");
</script>           
</tr>
</apex:repeat>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.