I'm going to rely on the saying that no question is a dumb question, but I have a slightly dumb one to ask.
EDIT:
Seems that this question has been asked and answered a few times on here already, though using titles I didn't come across when searching for duplicates. Here are some related posts:
Double to string conversion without scientific notation
How to convert double to string without the power to 10 representation (E-05)
Place with an answer (Jon Skeet): http://www.yoda.arachsys.com/csharp/DoubleConverter.cs
Original Question:
I've got an app that sometimes stores some decently small numbers, such as 0.000023425. These numbers get loaded up in TextBox controls for users to edit. The following contrived example shows my issue:
Dim number As Double = 0.000023425
someTextBox.Text = number.ToString() ' Shows "2.3425E-05"
As mentioned, the text that shows is 2.3425E-05, which isn't exactly intuitive for the user to see, plus I have numbers even more precise (out to 19 decimal places). I would like the output to be fixed point. Of course I could easily and cleanly do:
number.ToString("F20") ' Shows "0.00002342500000000000"
But then there's an annoying number of zeros left over. So, to fix that, I could do:
number.ToString("#,##0.####################") ' Shows "0.000023425"
Which is what I want, but is there any better way to do it than to have that giant ugly format string there? So, ok, it's not that ugly, but surely there's a different way, right? My goal is to show the raw value being stored in the DB in a friendly way, and I would rather not have to force a format on the number at all.
Thanks!
UPDATE
Perhaps I said a bit too much in the post. After some experimentation, I found that changing the underlying datatype to Decimal solves the issue.
Dim number As Decimal = 0.000023425
someTextBox.Text = number.ToString() ' Shows "0.000023425" instead of "2.3425E-05"
So it sounds like Doubles aren't precise enough to be displayed normally?
EDIT
I found a post (albeit with no upvotes) that simply casts the the Double value as a Decimal before doing a .ToString() on it, which has the desired effect. Is there any reason why I wouldn't want to do that? Perhaps the cast to Decimal has the potential to end with a different value than the Double (even though it's a negligible amount), so perhaps the #,##0.#################... format string is safer.
Make your format string a constant (say in your resource file) and use it by name. You avoid seeing the ugly format and gain consistency from control to control in your format (i.e., if you change your mind, all controls will gain the new look).
As an alternative (or in conjunction), derive a custom control from the text box and have properties that invoke the different formatting strings you wish to use in an easier syntax.
Either way your goal should be not repeat yourself... by placing the configuration in one place you avoid mistyping it eventually and having to track down a strange formatting bug.
We do something similar by creating "percent" and "currency" text controls that encapsulate all the formatting and parsing requirements. They work just like text controls otherwise.
Related
I'm having an issue where the API is translating the number 15 into the german literal string "fünfzehn" instead of leaving it as a number. It is only the number 15 and no other. It's formatted the same way as other numbers, nothing special about it.
We're sending html, so I had a look at the tags - not the issue either. We'd like to avoid wrapping numbers in a "notranslate" tag since it's only happening for 15.
Has anyone stumbled upon the same thing?
I'm not sure if anyone has faced this issue.
Basically I have a text document with about 100,000 lines and I am trying to import it into an SQLite table with a single column.
After doing so, when I did a generic query SELECT * FROM table WHERE field LIKE "%something%", I realised that irrelevant results were turning up. Digging further, the reason was that some of the lines in the original text file got concatenated into giant row entries. This gives the impression of a wrong result (which was simply due to the giant rows having a match). Instead of 100,000 records, I had only 50,000 odd and 2 records with LENGTH(field) > 1,000,000 characters.
The first thing that came to my mind was the possibility of special characters messing things up, so I did a strings FILE in BASH. The problem persisted.
So, long story short, does anyone know the reason for this (and how to solve the issue)? Considering that the table has a single field, I don't think delimiters have anything to do with this right?
I've traced the issue to unbalanced double-quotes reserved for quoting strings in csv. so if i have an open quote on one line, it will only count as a record when the next quote is found - which can be many lines down.
I've created a couple views showing e.g. the latest news and events. However, when the username contains "special characters" (but still common in Finland), for example 'ä' or 'ö', the view shows the empty text instead of the real data.
For example my events view has two fields (node title and time of the event) and three filters (published = yes, node type = event and event time >= now). Nothing closely related filtering with the username added by myself.
Is there a workaround to get the view working?
Sounds like a problem with character encoding. I would recommend UTF8, works with almost every language.
The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets
Whatever you do, make sure you use always the same encoding and use it everywhere.
Program chooses the number to be guessed by selecting an int at random in the range 1–100. The program then displays the following text in a label:
I have a number between 1 and 100 -- can you guess my number? Please enter your first guess.
A TextBox should be used to input the guess. As each guess is input, the background color should change to red or blue. Red indicates that the user is getting "warmer", blue that the user is getting "colder". A label should display either "Too High" or "Too Low", to help the user zero in on the correct answer. When the user guesses the correct answer, display "Correct!" in a message box, change the Form's background color to green and disable the TextBox. A TextBox (like other controls) can be disabled by setting the control's Enabled property to False. Provide a Button that allows the user to play the game again. When the button is clicked, generate a new random number, change the background to the default color and enable the TextBox.
You might want to start off by thinking of the structure, breaking everyting down into smaller pieces.
Step one - Identify the requirements
Now you said that you want to create some sort of software, that allowes you to guess on random numbers. So If we break this down into smaller pieces we get the following:
Random number Generator
Amount of guesses
Possibility to post a new guess
These are the minumum requirements, so if we break each step up into even smaller pieces we will get to the final solution pretty quickly ( i will however only provide you with a sufficient amount of information to get your started on your homework ).
Step two - Understanding random
You might want to head over here to read a little about Random numbers in C#, however as you have probably already guessed you need a ranom number generator, i've provided you with two links to ranom generators and information about it which should help you on the way, but to give you a little example here
example
Random generator = new Random();
generator.Next();
Now you have a couple of extra parameters that might come in handy, check the MSDN Guidelines on Ranom, there are methods / constructors that might be of interest which will help you select a ranom number between a and b.
Step three - creating the interface
Now this is where i say godbye to you, you should have sufficient information on how to start the solution and get some data out there. Otherwise I would suggest this resource
Break it up into smaller problems you can get started on.
Can you create a textbox and echo the input?
Can you generate a random number?
Can you test if a given number is greater or less than your chosen number?
Break the problem into smaller pieces.
You need the program to:
1. Ask user question.
2. Take input from user.
3. Process input from user.
Do the smallest thing that does something, no matter how uselss, like print("Guess my number"); then build it from there. Try doing it without using GUI components, they can distract you from the solution.
Once you have a working program that can guess a random number, then introduce GUI elements to it.
I'm using a JQuery plugin (http://docs.jquery.com/Plugins/Autocomplete) to add auto-completion to a "city" textfield. The component calls an ASP.NET page that simply loads an array of all possible city values (>8000) and then iterates that array returning those that start with the text the user has so far entered.
The thing is, it's pretty slow in real use. It lags behind what the user types to the extent that most of the time the user probably won't notice that it's there.
So, my question is, how can I speed it up?
I had thought that an array would be a better way to go than putting the data in a database and having to hit that multiple times. Do others agree that having this information hard-coded is the way to go given that it's not at all volatile and needs to be all about speed of return?
If so, what would you look at to improve the speed of performance? Should I be caching the data on application start and accessing it from memory? Would I be better off instead with multiple arrays, each containing values starting with a particular letter so I can go straight to the relevant one and thus iterate a much smaller array? Or am I missing a much more obvious way to go about this?
Thanks in advance.
The code you posted looks pretty normal and should return fast. My guess would be that you're using the default "delay" value for the plug-in which is 400ms. Try changing it to something lower like 100ms (or even lower) and see if it feels better:
$('#textbox').autocomplete('your_url_here', {
delay: 100
});
Well you probably don't want to hard code any data if you can avoid it. Having it in a database will make it much easier to manage changes. And there's no reason you would have to query the database each time. You could cache data from sql in memory into multiple lists as you touched on yourself. That should be quite speedy, unless there's some sort of additional network latency problem. You might want to post some of your code so people can provide more specific guidance.
Thanks for the reply. (Very basic) code is as follows:
Dim q As String = LCase(Server.UrlDecode(Request.QueryString("q")))
Dim arrCities() As String = {"Abano Terme", "Abbadia Cerreto", ... "Zungri"}
For Each s As String In arrCities
If s.ToLower.StartsWith(q) Then
Response.Write(s & vbCrLf)
End If
Next
Line 2 is basically a huge array declared right in the code. It isn't pretty, but the data is involatile so I thought I could get away with it and I assumed it would be faster than pulling it directly from the database. But maybe SQL with some caching if you can offer some specific advice?