Click here to Skip to main content
15,897,891 members
Home / Discussions / C#
   

C#

 
QuestionRecord RTSP live video from ip camera Pin
Emiliano15-Sep-09 6:21
Emiliano15-Sep-09 6:21 
QuestionHow to show only 2 decimal points when using a double Pin
Etienne_12315-Sep-09 4:29
Etienne_12315-Sep-09 4:29 
AnswerRe: How to show only 2 decimal points when using a double Pin
musefan15-Sep-09 4:35
musefan15-Sep-09 4:35 
AnswerRe: How to show only 2 decimal points when using a double Pin
Greg Chelstowski15-Sep-09 4:36
Greg Chelstowski15-Sep-09 4:36 
AnswerRe: How to show only 2 decimal points when using a double Pin
Luc Pattyn15-Sep-09 4:37
sitebuilderLuc Pattyn15-Sep-09 4:37 
AnswerRe: How to show only 2 decimal points when using a double Pin
Etienne_12315-Sep-09 4:40
Etienne_12315-Sep-09 4:40 
GeneralRe: How to show only 2 decimal points when using a double Pin
musefan15-Sep-09 4:55
musefan15-Sep-09 4:55 
AnswerRe: How to show only 2 decimal points when using a double Pin
0x3c015-Sep-09 5:32
0x3c015-Sep-09 5:32 
As far as I know, the framework doesn't provide this explicitly. However, if you really must return a truncated string, then I've whipped up an extension method for you:
C#
public static string ToDecimalPlace(this double i, int decimalPlaces)
{
    string toString = i.ToString();
    int decimalPoint = 0;

    if ((i % 1) == 0) //Is the number whole?
        return toString;
    decimalPoint = toString.IndexOf('.');
    // + 1 takes account of the zero-based index returned by IndexOf
    return toString.Substring(0, decimalPoint + decimalPlaces + 1);
}

I'm personally a little concerned about the strings being created, but I can't see a simple way around that problem. It will only usually show up under high load anyway. And remember to vet the input - this is only a proof of concept. If you do something like (8.1111111111111111111111111).ToDecimalPlace(840) you'll get an IndexOutOfRange exception.


GeneralRe: How to show only 2 decimal points when using a double Pin
J4amieC15-Sep-09 5:46
J4amieC15-Sep-09 5:46 
GeneralRe: How to show only 2 decimal points when using a double Pin
0x3c015-Sep-09 5:51
0x3c015-Sep-09 5:51 
AnswerRe: How to show only 2 decimal points when using a double Pin
OriginalGriff15-Sep-09 6:24
mveOriginalGriff15-Sep-09 6:24 
GeneralRe: How to show only 2 decimal points when using a double Pin
0x3c015-Sep-09 6:55
0x3c015-Sep-09 6:55 
GeneralRe: How to show only 2 decimal points when using a double Pin
OriginalGriff15-Sep-09 8:31
mveOriginalGriff15-Sep-09 8:31 
Questionhow to access and run method from main form without creating a new object instance of the form? Pin
stan_lee2615-Sep-09 3:34
stan_lee2615-Sep-09 3:34 
AnswerRe: how to access and run method from main form without creating a new object instance of the form? Pin
Keith Barrow15-Sep-09 4:18
professionalKeith Barrow15-Sep-09 4:18 
QuestionWindows 7 - Install Windows Service Pin
dataminers15-Sep-09 2:23
dataminers15-Sep-09 2:23 
AnswerRe: Windows 7 - Install Windows Service Pin
Mirko198015-Sep-09 2:27
Mirko198015-Sep-09 2:27 
GeneralRe: Windows 7 - Install Windows Service Pin
dataminers15-Sep-09 4:03
dataminers15-Sep-09 4:03 
QuestionExcel application object in automation add-in? Pin
SRKSHOME15-Sep-09 2:01
SRKSHOME15-Sep-09 2:01 
QuestionHow to improve preformance of drawing in c# Pin
Member 33751415-Sep-09 1:33
Member 33751415-Sep-09 1:33 
AnswerRe: How to improve preformance of drawing in c# Pin
Nuri Ismail15-Sep-09 1:47
Nuri Ismail15-Sep-09 1:47 
GeneralRe: How to improve preformance of drawing in c# Pin
Luc Pattyn15-Sep-09 4:39
sitebuilderLuc Pattyn15-Sep-09 4:39 
GeneralRe: How to improve preformance of drawing in c# Pin
Nuri Ismail15-Sep-09 4:59
Nuri Ismail15-Sep-09 4:59 
AnswerRe: How to improve preformance of drawing in c# Pin
musefan15-Sep-09 2:09
musefan15-Sep-09 2:09 
AnswerRe: How to improve preformance of drawing in c# Pin
Eddy Vluggen15-Sep-09 2:39
professionalEddy Vluggen15-Sep-09 2:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.