Skip to main content

Notifications

Microsoft Dynamics AX forum
Suggested answer

How to change the format of string value and return the string value with defined format

Posted on by 161
Hi developers ,
 
I have a string value '1200' , I need to change the format of this value using decimal as 2 and thousand operator as comma like '1,200.00'  and return value should be type str.
If we have entered value 100 it should display as '100.00'
 
I can't change the EDT property as it standard one. We need this change through code only.
Thanks!!
  • Martin Dráb Profile Picture
    Martin Dráb 227,979 Super User 2024 Season 2 on at
    How to change the format of string value and return the string value with defined format
    I'm sorry, but I can't point out to the bug in your code if I've never seen the code.
     
    But I show a few tricks to make easier.
     
    First of all, test code in smaller pieces. Don't bother integration your string formatting logic sales order posting if you don't know whether it works at all. You just make debugging much more slower and complicated for you.
     
    Instead, create a runnable class where you'll test your logic. It seems your code doesn't work correctly with values higher than 1000 (maybe because it can't handle strings with more than three characters), therefore that's the obvious scenario to start with. Run the class in debugger and check out where it fails. If it doesn't, you'll at least know that this piece of logic isn't responsible for your problem.
     
    Only when you have this piece of logic working, integrate it with other code and perform integration tests.
  • D365 ER Profile Picture
    D365 ER 161 on at
    How to change the format of string value and return the string value with defined format
    Error Message  :An unexpected client error has occurred.
    It occured if we have posted the transcation with sales price as more than 1000 and make one invoice proposal.
    under invoice proposal tab if we select line with sales price displaying value > 1000, and we switch to next line it is throwing the above.
    I guess its becoz I have applied num2str in that sales field as it is display str method. 
  • Martin Dráb Profile Picture
    Martin Dráb 227,979 Super User 2024 Season 2 on at
    How to change the format of string value and return the string value with defined format
    Please show me your code, tell me what kind of error you got (compilation error / runtime exception), what is the error message and where it is raised.
     
    I can't solve your problem if you tell my almost nothing about it.
  • D365 ER Profile Picture
    D365 ER 161 on at
    How to change the format of string value and return the string value with defined format
    Thanks Martin......Tried this way but when I am assigning this value to variable and jumping to another code it is giving error and on removing this function no error is coming. Apart from using num2str is there any way of doing this?
     
  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 227,979 Super User 2024 Season 2 on at
    How to change the format of string value and return the string value with defined format
    The native way in X++ is using num2Str(). For example: num2Str(1200, 0, 2, DecimalSeparator::Dot, ThousandSeparator::Comma). Convert your string to a number and then format the number to string.

    In more complex cases, I tend to use .NET formatting functions.
     
    Consider carefully whether you need a fixed format or you should respect receiver's culture (e.g. using comma as the decimal separator).

Helpful resources

Quick Links

Dynamics 365 Community Update

Welcome to the inaugural Community Platform Update. As part of our commitment to…

Dynamics 365 Community Newsletter - August 2024

Catch up on the latest D365 Community news

Community Spotlight of the Month

Kudos to Mohana Yadav!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,142 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 227,979 Super User 2024 Season 2

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans