|
|
My "question" was more philosophical than technical.
I have built ERP systems from the ground up that passed audit from national accounting firms. My "naive" approach to rounding was never a problem. (And this was "before" .NET)
The concept of randomly deciding to go up or down, is not "determinate" from an auditing point of view.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
There is a good option there which is not random: always round to the even number. Not being an accountant I don't know if auditors have their own rule.
|
|
|
|
|
I have worked at different fintech companies for 10 years.
At least in one part of the industry rounding was specifically regulated. It varied by locality. And over time.
|
|
|
|
|
Yes but that is accounting, not C#/.NET.
|
|
|
|
|
So what about a programming developing an accounting application in C#?
Religious freedom is the freedom to say that two plus two make five.
|
|
|
|
|
What would you do in any other language?
There isn't a magic-bullet solution to this problem in any language. If the rules are subject to change, then you either need to recompile, or have some means of configuring the code to use specific rounding rules in specific situations.
The only complaint with .NET / C# is if you're stuck with .NET Framework, where you only options are "to even" or "away from zero"; the options for "to zero", "to negative infinity", and "to positive infinity" were added in .NET Core 3.0.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Richard Deeming wrote: is if you're stuck with .NET Framework,
You can of course add code to potentially handle other situations.
Probably the most stringent part of this is that the developer should be able to show why they are using a specific rounding and then document it. Probably need to explain it as well. Both for potential audits as well as insuring future maintenance programmers do not 'fix' anything.
I have certainly had to explain this to non-developers before that should have already understood it since they were dealing with financial decisions daily.
This would always be the case when dealing with financial transactions. This can even become a problem when developers decide to store currency amounts in floating point data types. Then even something like addition can be impacted by rounding.
I never experienced a problem with scientific results but when I was programming for that I was not aware of any of the issues. To be fair back then even financial institutions did not seem aware of it. The theory was known but in had not made a transition to practical usage (in many cases.)
Richard Deeming wrote: If the rules are subject to change, then you either need to recompile,
It probably is going to be more complicated than that. For one time calculations then ok. But one must insure that that business rule will never change.
The case I encountered was where the company was using paper records printed years before. Sometimes those were lost. So they printed (ran a report) again. After the rules had changed. No one in the company was aware of this problem until I pointed it out once I needed to do an update on the report.
As you pointed out this required a dynamic solution which I implemented.
|
|
|
|
|
As with all programming development, the code should implement the business rules.
|
|
|
|
|
Not sure what you mean.
I presume there are accountants that still use paper ledgers. But the vast majority of accountants use software applications.
Consider any sort of interest bearing account. Savings, CDs, Loans, Mortgages, etc.
The interest is calculated on a schedule that is either defined by the institution or some other regulatory body. That happens in software (vast majority of cases.)
|
|
|
|
|
I merely pointed out to Gerry that .NET offers some options as to how numbers may be rounded, including the one to always round towards the even number. Whether he (or you) use that feature is a completely free choice, and has nothing to do with rules set by accounting, audit, or other business systems.
|
|
|
|
|
Hello everyone,
I have problem with char* from string (CharPointerFromString):
static int LengthSize(string str)
{
if (str == null)
{
return 0;
}
return (str.Length * 4) + 3;
}
static char* CharPointerFromString(string str)
{
if (str == null)
{
return null;
}
char[] charArr = new char[str.Length];
for (int i = 0; i < str.Length; i++)
{
charArr[i] = str.ToCharArray()[i];
}
fixed (char* charPtr = charArr)
{
return charPtr;
}
}
static int Main(string[] args)
{
printf(CharPointerFromString($"Hello {"DeafMan1983"}!\n"));
}
Result:
Hello DeafMan1983!
And I add string from char* (StringFromCharPointer)
static string StringFromCharPointer(char* s, bool freePtr = false)
{
if (s == null)
{
return string.Empty;
}
char* ptr = s;
while (*ptr != 0)
{
ptr++;
}
string result = Encoding.UTF8.GetString((byte*)s, (int) (ptr - s));
if (freePtr)
{
NativeMemory.Free(s);
}
return result;
}
But I have problem with extension like text".txt" -> if I don't have text.txt than I write simple with FILE and fopen(), ferror() and fclose();
string text_txt = Path.GetFileName("text.txt");
char* path = CharPointerFromString(text_txt);
FILE* file = fopen(path, CharPointerFromString("r"));
if (file == null)
{
printf(CharPointerFromString($"Error: {StringFromCharPointer(path)} not found.\n"));
return ferror(file);
}
printf(CharPointerFromString($"Success: {StringFromCharPointer(path)} found.\n"));
fclose(file);
Result: It seems forgetting extension?
Success: text found.
How do I fix it?
|
|
|
|
|
Is this Managed C++/CLI code?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
That's not C# code. That's C/C++ and you're in the frong forum.
|
|
|
|
|
That's not c/c++ it is C# why do you not expect class and variables are not c/c++ just in c# i have added pivoke from msvcrt.dll to DllImportAttribute.
|
|
|
|
|
That code is not C# it is all C/C++.
modified 20-Jan-24 6:57am.
|
|
|
|
|
Please read correctly! That's real C# because I wrote in pure C# because C/C++ and C# are not same like I wrote without const because I write only in C# and static int Main(string[] args) {...} it means not in C/C++ that's why You need to read carefully.
Check correct my code in starting post:
static int Main(string[] args)
{
printf(CharPointerFromString($"Hello {"DeafMan1983"}!\n"));
}
Where is C/C++ style?
int main (int argc, char** argv)
{
....
return 0;
}
Thanks!
|
|
|
|
|
You are right, I misread some parts.
|
|
|
|
|
It's okay, no worries! I respect everyone.
|
|
|
|
|
My bad. I saw the bottom cod e snippet and assumed the rest was the same language.
|
|
|
|
|
If you don't have a "path", then Path.GetFileName does nothing for you.
This makes more sense in the present context:
string text_txt = "text.txt";
(I didn't try to reproduce your issue).
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
Just a guess...
Try 'text12.txt' and see what happens.
|
|
|
|
|
|
Quote:
char[] charArr = new char[str.Length];
for (int i = 0; i < str.Length; i++)
{
charArr[i] = str.ToCharArray()[i];
} That code is horrendously inefficient!
Every call to the ToCharArray method[^] creates a new array containing a copy of every character in the string.
For a string of 100 characters, you are creating 101 copies of that string, just to return a single copy.
Either use:
char[] charArr = new char[str.Length];
for (int i = 0; i < str.Length; i++)
{
charArr[i] = str[i];
} or simply:
char[] charArr = str.ToCharArray();
Quote:
fixed (char* charPtr = charArr)
{
return charPtr;
} As suggested by the enclosing braces, the fixed keyword[^] pins a managed object until the end of the enclosing block.
You return the pointer from the method, which escapes the fixed block. Once you do that, there is no guarantee that the pointer will still be valid. The GC is free to move the array around in memory as much as it wants. Whilst it may appear to work in your simple test, you can guarantee that as soon as you try to do this in "real" code running on a production system, the GC will kick in at the most inconvenient moment, move your array, and leave you with either an impossible-to-diagnose crash, or worse, corrupt data.
It's not entirely clear what you're trying to do with these functions. But what is clear is that they're not going to do what you expect them to.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hello I am back.
I have tried but I am glad because I found solution.
namespace DeafMan1983.Interop.Runtime.Utilities;
using System;
public static unsafe class UtilitiesForUTF16
{
/*
* string from char* (UTF16)
*/
public static string CharPointerToString(char* ptr)
{
if (ptr == null)
return string.Empty;
<pre>
int length = 0;
while (ptr[length] != '\0')
{
length++;
}
return new string(ptr, 0, length);
}
/*
* char* (UTF16) from string
*/
public static char* StringToCharPointer(string input)
{
if (input == null)
return null;
char* utf16Ptr = stackalloc char[input.Length + 1];
fixed (char* inputPtr = input)
{
for (int i = 0; i < input.Length; i++)
{
utf16Ptr[i] = inputPtr[i];
}
inputPtr[input.Length] = '\0';
return inputPtr;
}
}
/*
* It works like strlen, but it uses only char* (UTF16)
/
public static int CharPointerLength(char charPtrs)
{
if (charPtrs == null)
return 0;
int length = 0;
while (charPtrs[length] != '\0')
{
length++;
}
return length;
}
}
And I resolved it:
1 Test:
Shows Console.WriteLine() :
string string_str1 = "Hello World!";
char* char_str1 = StringToCharPointer(string_str1);
Console.WriteLine($"Result: {CharPointerToString(char_str1)}");
Console.WriteLine($"Length of CharPointer: {CharPointerLength(char_str1)}");
char* char_str2 = stackalloc char[] { 'H', 'e', 'l', 'l', 'o' };
string str2 = CharPointerToString(char_str2);
Console.WriteLine($"Result: {str2}");
Console.WriteLine($"Length of CharPointer: {CharPointerLength(char_str2)}");
Picture 1
And I tested with WinAPI ( TerraFX.Interop.Windows )
I have tested it and it won't show Chinese Language
Picture 2
I am very excited to resolve it.
But I don't know if It works fine. I hope it works anything if UTF16 says wrong like Chinese Fonts show up then we need add some Encoding....
|
|
|
|