Options

How to format a decimal into a string?

nicolenicole Member Posts: 25
edited 2004-11-22 in Navision Attain
hi!... new to navision here... in C/AL code, how will i format a number with data type of decimal into a string?... the output should be a string and should always have 2 decimal places...

for example: the output of 5 should be 5.00 (in string)

thanks in advance for your help!

:D

Comments

  • Options
    fbfb Member Posts: 246
    There are internationalization issues here:
      1) Do you want the 'decimal point' to follow the convention for the user's locale (e.g. someplaces use 'dot', others use 'comma'), or do you always need a 'dot'? 2) Do you want the thousands to be grouped (e.g. 1,234.56), and if so, should the 'grouping symbol' follow the convention for the user's locale, or do you require a specific character?
    So, you need to experiment a little. Start with:
    MyString := FORMAT(MyDecimal);
    
    If this doesn't give you the right number of decimal places, try
    MyString := FORMAT(MyDecimal,0,'<Precision,2:2><Standard Format,0>');
    
    If you do not want thousands grouping, use one of the following:
    MyString := FORMAT(MyDecimal,0,2);
    MyString := FORMAT(MyDecimal,0,'<Precision,2:2><Standard Format,2>');
    
    Finally (or perhaps better 'first') see the on-line help 'C/SIDE Reference Guide' topic on the Format property and the FORMAT statement.
  • Options
    nicolenicole Member Posts: 25
    thanks fb! :D
  • Options
    PaLPiTaTioNPaLPiTaTioN Member Posts: 55
    edited 2004-11-22
    I try to round the decimal variable decVariable to exactly one decimal, but it just won't work. I use this syntax:
    FORMAT(decVariable,0,'<Precision,1:1><Sign><Integer Thousand><Decimals>')
    

    And I get these results:
    • 484
    • 199.5
    • 422
    • 1034.50
    • 1246.00
    etc.

    As you can see, some numbers are converted to two decimals even when the original number is an integer value. What am I doing wrong?
  • Options
    jmjm Member Posts: 156
    Hi PaLPiTaTioN,

    i have tested your code and at me it works.

    how did you test?

    br
    Josef Metz
    br
    Josef Metz
  • Options
    PaLPiTaTioNPaLPiTaTioN Member Posts: 55
    It's really odd.

    The string output is written to ascii. The thing is that my output is being written to a csv-file which then is read by Excel. I used an hex-editor to doublecheck the contents of the csv-file, and in that file the output is correct. So the Navision-functionality is alright.

    Danke!
Sign In or Register to comment.