Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Suggested answer

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

Posted on by 167
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 230,214 Most Valuable Professional 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 167 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 230,214 Most Valuable Professional 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 167 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 230,214 Most Valuable Professional 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).

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans