I have the need to preserve the precision of a user-entered number, but that
needs to be stored in a (non-string) floating point variable (such as double or
decimal). Due to the internal limitations of numerical precision of the double
type, I'm presuming that the value will be stored in a decimal for the time
being.
Supposing the following:
decimal d1 = 123.45;
decimal d2 = 888.1234567;
decimal d3 = 677.100;
decimal d4 = 1.0;
Now, when converting to string, I want the following:
string s1 = d1.ToString(/*format???*/); // Should be "123.45"
string s2 = d2.ToString(/*format???*/); // Should be "888.123456 7"
string s3 = d3.ToString(/*format???*/); // Can be "677.1"
string s4 = d4.ToString(/*format???*/); // Should be "1";
I don't want s1 to be something like:
"123.450000 00"
and I don't want s2 to be truncated to something like:
"888.123"
Get what I'm after? Is there a format string that will handle this?
If not, what I currently have is to simply string.Trim("0" ) the trailing zeros
and then string.Trim("." ) to trim a lagging decimal. 1 3907
Julie wrote: I have the need to preserve the precision of a user-entered number, but that needs to be stored in a (non-string) floating point variable (such as double or decimal). Due to the internal limitations of numerical precision of the double type, I'm presuming that the value will be stored in a decimal for the time being.
Supposing the following:
decimal d1 = 123.45; decimal d2 = 888.1234567; decimal d3 = 677.100; decimal d4 = 1.0;
Now, when converting to string, I want the following:
string s1 = d1.ToString(/*format???*/); // Should be "123.45" string s2 = d2.ToString(/*format???*/); // Should be "888.123456 7" string s3 = d3.ToString(/*format???*/); // Can be "677.1" string s4 = d4.ToString(/*format???*/); // Should be "1";
I don't want s1 to be something like:
"123.450000 00"
and I don't want s2 to be truncated to something like:
"888.123"
Get what I'm after? Is there a format string that will handle this?
If not, what I currently have is to simply string.Trim("0" ) the trailing zeros and then string.Trim("." ) to trim a lagging decimal.
Found the answer myself. A format string of "G" looks like the solution, just
didn't see it first time around in the string formatting docs. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Roger Leigh |
last post by:
Hello,
I'm writing a fixed-precision floating point class, based on the ideas
in the example fixed_pt class in the "Practical C++ Programming" book
by Steve Oualline (O' Reilly). This uses a long int to store the
value, and the precision (number of decimal points) is variable (it's
a templated class):
template <size_t _decimal_places = 4>
class FixedFloat {
|
by: Brian van den Broek |
last post by:
Hi all,
I guess it is more of a maths question than a programming one, but it
involves use of the decimal module, so here goes:
As a self-directed learning exercise I've been working on a script to
convert numbers to arbitrary bases. It aims to take any of whole
numbers (python ints, longs, or Decimals), rational numbers (n / m n,
m whole) and floating points (the best I can do for reals), and
convert them to any base between 2 and...
|
by: Michel |
last post by:
Hi there,
I need to make a poisson distribution function that uses:
double Math.Exp(double d)
The d argument is a negative number in my case. When d becomes bigger and
bigger, the result loses precision and is finally 0 (when d reaches 650)
because it is too small to fit in a double. In my case, d can become -10000,
so I need to have a greater precision number and Exp function.
|
by: Bryan R. Meyer |
last post by:
I am a relatively new C++ programmer and am attempting to write a
function that takes a number of type float and adds commas to it in
the appropriate places. In order to manipulate the number to add the
commas, I convert it to a string (specifically a char rather than an
actual string object) using the
gcvt function as shown below.
char amt;
gcvt(amount,50,amt);
|
by: adrin |
last post by:
hello,
how can i make a conversion from a string to a multiple precision integer
stored in memory in a base 2^32?
i guess conversion from integer to string would require dividing modulo 10,
but how do the opposite conversion?
| |
by: BigMan |
last post by:
How can I check if assignment of a float to a double (or vice versa)
will result in loss of precision?
|
by: towers |
last post by:
Hello,
I've got a bit of experience in C++, but I'm writing my first app that
is dependent on relatively precise math functions. The app requires
that I get a time stamp based on s sample number, from a time series.
This seems liek an easy thing to do:
long lSample = 500; (for example)
double dSampleRate = 1000.0;
|
by: Julie |
last post by:
I have the need to preserve the precision of a user-entered number, but that
needs to be stored in a (non-string) floating point variable (such as double or
decimal). Due to the internal limitations of numerical precision of the double
type, I'm presuming that the value will be stored in a decimal for the time
being.
Supposing the following:
decimal d1 = 123.45;
decimal d2 = 888.1234567;
|
by: Matthew |
last post by:
Hi,
I want to change the precision level of floating point variables and
calculations which is done in php.ini.
However the server I rent for my domain does not give me access to
php.ini, they say 'for security reasons'.
Can the precision level be changed by PHP code as needed?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |