Click here to Skip to main content
15,894,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, i have this code:
C#
private void button1_Click(object sender, EventArgs e)
        {

            double number =Convert.ToDouble( textBox1.Text);
            string s = (Math.Round(number, 3)*1000).ToString();

            int norm=1;
            for(int i=0;i<=s.Length-1;i++)
            {
                norm*=10;
            }
            label1.Text =Math.Round((Convert.ToDouble(s)/norm),3).ToString() ;

        }

this should bring a number like 11256,125698 to 1,125 or 18668951 to 1,866 (theorethicaly) and so on.
Theorethical works (at least for my mind) and with small numbers also but when i tried on 18668951 it gaved me an unexpected rezult(15.356). Can anyone tell me where i messed up? (double isnt enough to work with such big number?)
Posted

No, when I tried it:
C#
double number = Convert.ToDouble("18668951");
string s = (Math.Round(number, 3) * 1000).ToString();

It gave s as "18668951000"
So you are trying to create an int (32 bits) which overflows.
Try making norm an Int64 instead.

[edit]Typo - OriginalGriff[/edit]
 
Share this answer
 
v2
Comments
aryx123 24-Feb-12 7:08am    
Yep you are right there was an overflow (didnt expect that... i guess its shows that i started programing a few weeks ago). Thank you so much!
OriginalGriff 24-Feb-12 7:09am    
You're welcome!
What's wrong with:
C#
double l = Math.Ceiling(Math.Log10(number));
double n = number / Math.Pow(10,l-1);
label1.Text = Math.Round(n,3).ToString();

?

Note (if needed) you have to handle as special cases the powers of ten (e.g. 10, 100, 1000).
 
Share this answer
 
v2
Comments
aryx123 24-Feb-12 7:09am    
i have a question what is that d (its number of digits?)
double n = d / Math.Pow(10,l-1);
CPallini 24-Feb-12 7:39am    
Nope. It was a (cut & paste) mistake. Sorry for that.
aryx123 24-Feb-12 7:52am    
o ok now it works. It`s nothing wrong with your solution (maby a small step back @ power of 10 that i have to deal with but works if my number isnt a power of 10 (0.5% of cases) so its ok) just that i didnt saw it (i`m newbie in programming and i cant see allways the the simplest solution, most of the time i cant see it at all). Thank you so much for solution !
Hello,
Try this link where you have many solutions for your problem

how to formate textbox value .....in window application[^]

Allso I sugest that you simplify code :

Use variable type of double for value of "Convert.ToDouble(s)" because it is too complex inside Math.Round()
Sometimes this statment
C#
for(int i=0;i<=s.Length-1;i++)

won't work correct ( I still didn't find out why ) so I use
C#
for(int i=0;i<=s.Length-1;i=i+1)

And you have error in selecting type of int for variable norm, it should be type of double because int type is too small for a range of numbers that may appear during the work of program.
Avoid variable declaration in the same command that you use in the program,
exmpl :
C#
for(int i=0;i<=s.Length-1;i++)


You allso have error in condition
for stopping the for..next loop, it should be i < s.Lenght-1


I have changed your code so now it gives this result for your example 18668951 to 1,867 or 11256,125698 to 1,126 and it should be like that.

It looks like this :

C#
void Button1Click(object sender, EventArgs e)
{
    //
    // Declaration of variables
    //
    string s = "";
    double norm = 0;
    double number = 0;
    double s1 = 0;
    int i = 0;
    int lenght = 0;

        s = textBox1.Text;
        number = Convert.ToDouble(s);
        s1 = Math.Round(number,3)*1000;
        lenght = s1.ToString().Length;
        norm=1;
        for(i=0;i<lenght-1;i=i+1)
        {
            norm = norm * 10;
        }
        label1.Text =Math.Round((s1/norm),3).ToString() ;
}


You should allso take care of relevance of entered number , if it is not number at all Convert.ToDouble(s) won't work.






Al the best
Perić Željko
 
Share this answer
 
v6
Comments
aryx123 24-Feb-12 8:11am    
Thank you for tips and solution! and you are right for(int i=0;i<=s.Length-1;i++) its wrong but i`m beginner in programing so when i get an wrong rezult first i go to loop(such it was in this case). I changed that in <= cuz the rezult was 15._ _ _ not 1._ _ _ so i thought that there was the problem i change it and forgot about it(found it a while ago but thanks). Thank again for tips.
Since you are just reformating a string you may could use:

C#
string strNormalized = String.Format("{0:E}", Double.Parse(strNumberInTextBox));


But I think you don't want to show the exponent?
A possible function could be defined like this (others showed it)

C#
public static string GetNormalizedString(string strValue)
        {
            double dValue = Double.Parse(strValue, CultureInfo.InvariantCulture); // If you are not shure strValue is a double, use TryParse instead

            // 1. Get the "magnitude" of our number
            double dLog = Math.Floor(Math.Log10(dValue));
            // 2. Divide through the found magnitude
            double dResult = dValue / Math.Pow(10, dLog);

            // (Re-)format the result - in this case with rounding - maybe you just want to cut the value - leave Math.Round call away
            return String.Format("{0:F3}", Math.Round(dResult, 3));
        }


Attention: In the code you showed you do some not needed string conversions (bad performance). And think about what you do with the converted value later in your app, because you are loosing precision (maybe you want to hold the "real" value in a seperate variable for later use in calculations...)
 
Share this answer
 
v2
Comments
aryx123 24-Feb-12 8:30am    
You are right that string conversion is not needed but it was the first solution that i saw so...(i gues it could be forgiven) as for precizion i apply this normalize after calculation (only for showing rezults) so it cant do any harm.As for string it will not be a string at all the piece of code was from the code used to test my problem (it will be applied to a double and will return a double).
Thank you for solution! and for tip

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900