|
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....
|
|
|
|
|
I have in my view model the following code:
public DateTime? Activation_Date { get; set; }
public DateTime? Deactivation_Date { get; set; }
On the form, if I put the date as "abc" and click Save, I get the following validation error:
The value 'abc' is not valid for Activation_Date.
What I simply want is to modify Activation_Date. I want it to be "Activation Date" so the message will look like below. The end user doesn't want the underscore.
The value 'abc' is not valid for Activation Date.
View Code to display error messages
@Html.ValidationMessageFor(s => s.Activation_Date)
What is the technique to make this kind of change ?
|
|
|
|
|
The ValidationMessageFor extension method has an override that accepts a custom validation message as the second parameter.
@Html.ValidationMessageFor(s => s.Activation_Date, "Where we're going Marty, we don't need roads");
|
|
|
|
|
Hi Pete,
It doesn't work for me.
@Html.ValidationMessageFor(s => s.Activation_Date, "Activation Date is invalid");
1. I always have "Activation Date is invalid" visible beneath the activation date input field
2. Secondly, I have other validation error messages in a Validator class for Activation_Date field
RuleFor(x => x.Activation_Date).NotEmpty().WithMessage("Activation Date is required");
RuleFor(x => x.Activation_Date).GreaterThanOrEqualTo(DateTime.Today).WithMessage("Activation Date must be greater than or equal to the current date");
|
|
|
|
|
You appear to be mixing FluentValidation with ASP MVC Validation. I would choose one and stick with that. In this case, FluentValidation is going to be more flexible for you.
|
|
|
|
|
I have this code
public static void Main(string[] args)
{
var dir = @"C:\temp\TestDir";
PrintDirectoryTree(dir, 2, new string[] { "folder3" });
}
public static void PrintDirectoryTree(string directory, int lvl, string[] excludedFolders = null, string lvlSeperator = "")
{
excludedFolders = excludedFolders ?? new string[0];
foreach (string f in Directory.GetFiles(directory))
{
Console.WriteLine(lvlSeperator + Path.GetFileName(f));
}
foreach (string d in Directory.GetDirectories(directory))
{
Console.WriteLine(lvlSeperator + "-" + Path.GetFileName(d));
if (lvl > 0 && Array.IndexOf(excludedFolders, Path.GetFileName(d)) < 0)
{
PrintDirectoryTree(d, lvl - 1, excludedFolders, lvlSeperator + " ");
}
}
}
How can I display just the first 10 files in all directories found
|
|
|
|
|
Your requirements are not exact.
First you might want to be sure exactly what you mean by "first". That depends on ordering. And if you don't specify/define the ordering then the OS/apis will provide it in whatever order it wants.
Second it is not clear if you want 10 total files or 10 files per directory. But regardless you are going to need something that keeps count.
|
|
|
|
|
There are several ways to do that: the most basic is to add a count to your loop:
int showCount = 10;
foreach (string f in Directory.GetFiles(directory))
{
Console.WriteLine(lvlSeperator + Path.GetFileName(f));
if (--showCount <= 0) break;
}
A better way is to use the array as an Enumerable and only process ten items:
foreach (string f in Directory.GetFiles(directory).Take(10))
{
Console.WriteLine(lvlSeperator + Path.GetFileName(f));
}
But ... the order in which Directory.GetFiles "delivers" files is not defined by the method definition: The order of the returned file names is not guaranteed; use the Sort method if a specific sort order is required. So you need first decide what are "the first ten" and sort them according to that decision: name order? Create date? Modification date? Size? Ascending or descending?
We can't make that decision for you: we don't know your app!
You should also be aware that Directory.GetFiles has a number of overloads, one of which allows you to return all files in all subdirectories in a single call: Directory.GetFiles Method Overload 3[^] which may improve your app performance with large directory structures.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|