Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6833341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:57:59+00:00 2026-05-26T22:57:59+00:00

What is the best way to convert/parse a string into a ulong in c#

  • 0

What is the best way to convert/parse a string into a ulong in c# and keep precision ?
Direct cast is not possible, and the Convert class utility is not providing ulong conversion,
so I used an intermediate decimal variable, but I am losing precision.

decimal d  = Decimal.Parse("1.0316584"));
Console.Write(d) // displays 1.0316584
ulong u = (ulong)d;
Console.Write(u) // displays 1 , precision is lost

I first tried to use a long parser, but I got thrown out :

long l  = Int64.Parse("1.0316584")); // throws System.FormatException

EDIT :
Ok sorry my bad : My question was very badly put. “long” is indeed an integer in C# I was confused by other previously used languages. Plus, I had to use ulong because this is what the third party code I am using requests.So the multiplying factor as suggested in an answer was indeed the way to go

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:57:59+00:00Added an answer on May 26, 2026 at 10:57 pm

    Strings can store a lot more data than a long. If you convert to a long, you run the risk of not being able to convert it back.

    e.g. if I have the string Now is the time for all good men to come to the aid of their party. that can’t really be converted to a long. “Precision” will be lost.

    Having said that, A long is a 64 bit integer. It can’t store that kind of data unless you’re willing to change “encoding” somehow. If you have code that looks like this:

    decimal d  = Decimal.Parse("1.0316584"));
    Console.Write(d) // displays 1.0316584
    ulong u = (ulong)(d * 1000000000m);
    Console.Write(u / 1000000000m) // displays 1.0316584? Precision is not lost.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the best way to convert a (properly Json-formatted) String object into a
Possible Duplicate: Best way to convert an ArrayList to a string I have an
What is the best way to convert a UTC datetime into local datetime. It
What's the best way to convert a string to an enumeration value in C#?
What's the best way to convert search terms entered by a user, into a
What is the best way to convert any currency into decimal in C#? public
What's the best way to convert XML into Java objects? I don't want a
In Ruby, what's the best way to convert a string of the format: {
What is the best way to parse INI file in Perl and convert it
What is best way to convert date from JavaScript string in format YYYYMMDD to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.