Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
If the user enters - 6,500.20, would this then be converted to 6.50020?

What I have tried:

C#
double convertedValue;
            if (double.TryParse(value, out convertedValue))
            {
                return convertedValue;
            }

Output - 6500.20, but I need 6.50020
Posted
Updated 15-Feb-22 10:07am
v3
Comments
[no name] 15-Feb-22 2:14am    
here 6,5000.20 is in european
six thousand five hundred tonnes and 20 kilo's
i need to store as 6.50020
Maciej Los 15-Feb-22 11:18am    
Well, as far as i know, six thousand five hundred tonnes and 20 kilos is equal to 6520, instead of 650020.

1 solution

Depends on the setting on your particular machine: 6,500.20 in the UK would be six thousand five hundred point 2.
But in Germany, the dot and comma are reversed: six point five zero zero two.

To read a number in a specific format, use the TryParse overload that allows you to specify the culture: TryParse(String, NumberStyles, IFormatProvider, Double)[^]
 
Share this answer
 
Comments
[no name] 15-Feb-22 2:14am    
here 6,5000.20 is in european
six thousand five hundred tonnes and 20 kilo's
i need to store as 6.50020
OriginalGriff 15-Feb-22 2:48am    
And?
What is stopping you from doing that?

I'm not trying to be awkward, you just aren't giving us any information - remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with - we get no other context for your project.
[no name] 15-Feb-22 3:56am    
which cultureinfo i need to use. i tried "en-GB", "fr-FR" and es-ES but nothing worked. i need the output as 6.50020 but i am getting 6500.20
OriginalGriff 15-Feb-22 4:08am    
It works for me:
            string inp = "6,500.20";
            double result;
            if (double.TryParse(inp, NumberStyles.Any, new CultureInfo("en-GB"), out result))
                {
                Console.WriteLine(result);
                }
Prints "6500.2"

What does it give you?
[no name] 15-Feb-22 4:12am    
Gives the same but i need 6.50020

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