|
I have a decimal number with a variable amount of digits following the decimal. I need to make the number a whole number and in doing so I need to keep track of how many decimal places there were.
ie change 28.9876 to 289874 and store there were 4 decimal places.
I am currently doing this by converting to a string and checking how many are after the '.'
This seems a poor way to solve this problem, but I am drawing a blank on other approaches. Can anyone offer other more efficient/elegant solutions?
this thing looks like it was written by an epileptic ferret
Dave Kreskowiak
|
|
|
|
|
I'm sure there is an even better way, but...
decimal d = 10.1234;
int dp = (d - decimal.Truncate(d)).ToString().Length - 2;
int val = (int)(d * (decimal)Math.Pow(10, dp));
This method, although still converting it to a string, doesn't 'search' the string for a particular character... just gets its length... which is relatively efficient.
As I said... there will be a better way... what it is I cannot immediately say.
Hope this helps.
Matthew Butler
|
|
|
|
|
For instance
decimal d = 28.9876M;
int[] bits = Decimal.GetBits(d);
int pow = (bits[3] >> 16) & 0xFF;
At the end pow contains 4 , i.e. the number of digits after the decimal point.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
Appreciate the help
this thing looks like it was written by an epileptic ferret
Dave Kreskowiak
|
|
|
|
|
|
Thanks for the help.
this thing looks like it was written by an epileptic ferret
Dave Kreskowiak
|
|
|
|
|
Hi,
I m working on DNet Remoting to develop trading application to accept bid/ask value from user and broadcast to every connected user.
Process is working smoothly on localhost as well as on LAN.
But it is not working when i host app on static IP based server.Client send bid/ask value to server but does not get updates except client running on server.
Currently i m running server app manually by clicking on server.exe.
I did not get any change if I replace http channel with tcp
Any comment/suggestions r invited. Thanks all.
Client and Server Configuration file are given below.
Server Config File
-----------------------
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application name="Trading">
<service>
<wellknown mode="Singleton" type="Server.Broadcaster, Server" objectUri="Broadcaster.soap"/>
</service>
<channels>
<channel ref="http" port="16784">
<serverProviders>
<formatter ref="binary" typeFilterLevel="Full"/>
</serverProviders>
<clientProviders>
<formatter ref="binary"/>
</clientProviders>
</channel>
</channels>
</application>
</system.runtime.remoting>
</configuration>
Client Config file
-----------------------
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application name="EventClient.exe">
<client>
<wellknown type="Common.IBroadcaster, Common" url="http://203.200.95.133:16784/Trading/Broadcaster.soap"/>
</client>
<channels>
<channel ref="http" port="0">
<serverProviders>
<formatter ref="binary" typeFilterLevel="Full"/>
</serverProviders>
<clientProviders>
<formatter ref="binary"/>
</clientProviders>
</channel>
</channels>
</application>
</system.runtime.remoting>
</configuration>
|
|
|
|
|
Make sure port 16784 isn't blocked by your firewall.
|
|
|
|
|
Hi Guys. Maybe somebody here can help me. I'm writing a little app that writes records from a text file to an Excel file. However, I am writing data to two columns, namely A2 and B2. Problem here is that Every record gets appended to the same cell A2 and B2 instead of going onto writing the next record to A3 and B3 etc. Any ideas what I'm doing wrong here?
Code below:
foreach (ReadFile nomread in res)
{
objRange = objSheet.get_Range("A2", System.Reflection.Missing.Value);
objRange.Value2 += nomread.CLIENTSURNAME;
objRange = objSheet.get_Range("B2", System.Reflection.Missing.Value);
objRange.Value2 += nomread.CLIENTFORENAMES;
}
Excellence is doing ordinary things extraordinarily well.
|
|
|
|
|
Kwagga wrote: objRange = objSheet.get_Range("A2", System.Reflection.Missing.Value);
Kwagga wrote: objRange = objSheet.get_Range("B2", System.Reflection.Missing.Value);
This is what you're doing wrong. You are selecting same cell in loop, so you will get different value from nomread but it will write in same cell.
There are two ways you can write values to excel cell. One is using two dimensional array which would look something like yourArray[2,2] and you would send that to your excel worksheet. But in your code it seems like you don't know the max number or cells you will need. So for something like this you can use Array and Column index to write array.
The code will looks something like this:-
int iR, iC;
iR = 1;
iC = 1;
foreach (ReadFile nomread in res)
{
objSheet.Cells[iR, iC] = nomread.CLIENTSURNAME;
objSheet.Cells[iR, iC+1] = nomread.CLIENTFORENAMES;
iC = 1;
iR++;
}
I haven't tested this code so you may need to make some changes. But the point is you should get an idea from this...
- Stop thinking in terms of limitations and start thinking in terms of possibilities -
|
|
|
|
|
Thanks mate. It doesn't write all the records though. Only writes the last record. I get the jist of it and will see what I can do with what you provided me.
Excellence is doing ordinary things extraordinarily well.
|
|
|
|
|
Kwagga wrote: It doesn't write all the records though. Only writes the last record. I get the jist of it
What do you mean by it doesn't write all the records?? Can you post the code that you're using now??!
- Stop thinking in terms of limitations and start thinking in terms of possibilities -
|
|
|
|
|
Hi Mate. It works fine. I missed something from your sample.
Excellence is doing ordinary things extraordinarily well.
|
|
|
|
|
Okay
- Stop thinking in terms of limitations and start thinking in terms of possibilities -
|
|
|
|
|
Mate, what if I have more than just the two columns??
Excellence is doing ordinary things extraordinarily well.
|
|
|
|
|
It depends on your set of data. For example if I had 1000 random numbers in collection I would just increment iC column index. And when the column index hits 50 I would increment iR (row Index) and set iC (column Index) back to 1.
This is the code I used before for one my college projects. BUT KEEP IN MIND THAT IT HAS RANDOM NUMBERS ONLY. AND ORDER DOES NOT MATTERS IN THIS.
int iR, iC;
iR = 1;
iC = 0;
for (int i = 0; i <= Globals.Vars.arVbNums.Count - 1; i++)
{
iC++;
if (i % 30 == 0 && i != 0)
{
iR++;
iC = 1;
}
oSheet.Cells[iR, iC] = Globals.Vars.arVbNums[i];
}
But if you're dealing with something like, Data base where order of values matters you will have to stick to + operator. If you look at the code I gave you before it has something like [iR, iC+1] so you will just write something like [iR, iC+2] for next column.
- Stop thinking in terms of limitations and start thinking in terms of possibilities -
|
|
|
|
|
Hi
I m developing an application in C#. I m recording a video from Webcam and saving it on harddisk. it creates .avi file, which is very heavy file. so I want to convert it into .mpeg or any other format in which my video file is not as much heavy as .avi.
so plz tell me the solution or code for conversion.
Regardz
Shanzay
|
|
|
|
|
If you're looking for a full solution, and don't actually have a programming issue - gotta have actual code to have a programming issue right? - then you're far better off looking for bulk code on sourceforge.[^] You will probably have far better luck. Good luck.
Scott P
“It is practically impossible to teach good programming to students that have had a prior exposure to BASIC: as potential programmers they are mentally mutilated beyond hope of regeneration.”
-Edsger Dijkstra
|
|
|
|
|
Hi all.
I know i know first google then searching the forum and then ask stupid questions here we go:
I have a c# project which i can compile and run under Win2k. Now or company finally get XP ( yes )
Now i got a second machine were i should test my systems. Now i can compile this application under XP but when i want to run the app
...this application has failed to start because the application configuration is incorrect...
error appears. I searched through the manifest and found nothing unusual.
[edit] I also went trough the app with the dependcy walker found the DWMAPI.DLL which could not be found any suggestions about that?
if you have any idea or article or any other clue please let me know
thanks
Mathias
modified on Friday, May 30, 2008 8:06 AM
|
|
|
|
|
hi
i want to report from my data with following code :
string htmText = System.IO.File.ReadAllText("Report\\report.htm");
htmText = htmText.Replace("[name]", this.txtName.Text);
htmText = htmText.Replace("[family]", this.txtFamily.Text);
htmText = htmText.Replace("[id]", this.txtStudentNo.Text);
this.webBrowser1.DocumentText = htmText;
this.webBrowser1.ShowPrintPreviewDialog();
it works good, but, at runTime, the printPreviewDialog has a button named 'HeaderFooter' that show header and footer info, is there any way to access HeaderFooter's text and change it by code ?
thanks
|
|
|
|
|
Hello,
I have a strange problem using typed datasets.
I have the following datatable in a typed dataset
ChainDetail
-----------
ChainID
Code
Description
I've populated an instance of the datatable with 2 rows loaded from SQL server.
Here's my problem...
When I either use DataTable.Select or create a DataView and apply a filter to retrieve one row, the select/filter work fine however the datarow returned has FOUR columns.
[0] contains ChainID
[1] contains Code
[2] contains {}
[3] contains Description
This means that I can't cast this to a ChainDetailDataRow, as column [2] should be Description.
Any help or suggestions will be much appreciated!
Thanks
A Very Confused Blakey404
|
|
|
|
|
Hi, how many columns are returned from the query that populates the datatable? if you're using 'SELECT * FROM...' change it to 'SELECT ChainID, Code, Description FROM...'
Perhaps you could post some of the code if this hasn't helped
"An eye for an eye only ends up making the whole world blind"
|
|
|
|
|
This is the strangest thing...
That is exacly what my query looks like, its in a stored procedure.
When i populate the datatable the three columns are there and correct...
its only when i create a dataview or use .Select that the extra colum is inserted into my results!!
i'm at home now but i will connect to work later on and give a code example...
cheers for the reply
|
|
|
|
|
I've just had a brainwave...
This datatable is populating a combobox, directly via databinding (datasource, datavalue etc)
To clear the combo box i am setting it's index to -1...
could this or something else related to the combobox be inserting my extra column?
i bet it is. crap that's going to be fun to find.
|
|
|
|
|
The databinding shouldn't be the problem here as that's very much a read-only action... then again, I've had all sorts of problems with the comboBox and datasource properties! Try removing the selection using .SelectedItem = null instead. Can you step through the code and find the point where the extra column is added?
Good luck! Rob
"An eye for an eye only ends up making the whole world blind"
|
|
|
|