Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number to String in a formula field

I am using a formula field to concatonate 2 decimal values separated by a dash. However, I want the result to trim all unneccesary trailing zeros and decimal points for both values.

For example, I want values 10 and 8.5 to be "10 - 8.5". Now it shows "10.00 - 8.50".

The formula I am using is CSTR({field1}) + " - " + CSTR({field2}).

like image 926
Mike Cole Avatar asked May 15 '09 16:05

Mike Cole


People also ask

How do I convert a number to text in a formula field in Salesforce?

Converting to and from Text ValuesTEXT() converts a Percent, Number, Date, Date/Time, picklist, or Currency field into Text. TEXT() returns output without any formatting, commas, or currency signs. For example, TEXT(percent_value), if percent_value is set to 30%, returns 0.3.

How do I apply a string to a formula field in Salesforce?

When entering a formula, text strings must be enclosed in double straight quotes ( "This is a string." ). Column names must be enclosed in square brackets ( [Opportunity_Name] ). Returns a string by concatenating the values of the specified columns and input strings.

Can we change number field to text field in Salesforce?

In Salesforce Knowledge article types, the file field type can't be converted into other data types. You can't change the data type of a custom field referenced by other items in Setup such as Visualforce pages, Apex code, processes, or flows. Changing a custom field type can require changing many records at once.

Can we change formula field to text field?

No, its not possible. Formula fields are a read-only fields that cannot be converted to any other data type. Likewise, you cannot convert any other field type into a formula field.


3 Answers

I believe this is what you're looking for:

Convert Decimal Numbers to Text showing only the non-zero decimals

Especially this line might be helpful:

StringVar text     :=  Totext ( {Your.NumberField} , 6 , ""  )  ;

The first parameter is the decimal to be converted, the second parameter is the number of decimal places and the third parameter is the separator for thousands/millions etc.

like image 114
Sam Trost Avatar answered Oct 14 '22 14:10

Sam Trost


CSTR({number_field}, 0, '')

The second placeholder is for decimals.

The last placeholder is for thousands separator.

like image 31
Paul Grimes Avatar answered Oct 14 '22 15:10

Paul Grimes


i wrote a simple function for this:

Function (stringVar param)
(
    Local stringVar oneChar := '0';
    Local numberVar strLen := Length(param);
    Local numberVar index := strLen;

    oneChar = param[strLen];

    while index > 0 and oneChar = '0' do
    (
        oneChar := param[index];
        index := index - 1;
    );

    Left(param , index + 1);
)
like image 43
Robert Niestroj Avatar answered Oct 14 '22 13:10

Robert Niestroj