Convert number to fraction using symbols like ¼ ½ ¾ (in google spreadsheets) - javascript

I would like to convert a number to a fraction in Google spreadsheets (I'm on the latest Firefox, Windows 7). After some searching I managed to find this formula which works well for me (It converts cell A1 into a fraction):
=IF(INT(A1)=A1,A1,IF(INT(A1)>0 ; INT(A1) &" " ; "")&(INDEX(SPLIT((TEXT( MOD(A1;1);"000000000000000E000")); "E");1;1))/GCD((INDEX(SPLIT((TEXT( MOD(A1;1); "000000000000000E000"));"E");1;1));(10^(-1*INDEX(SPLIT((TEXT( MOD(A1;1);"000000000000000E000")); "E");1;2))))&"/"& (10^(-1*INDEX(SPLIT((TEXT( MOD(A1;1);"000000000000000E000")); "E");1;2)))/GCD((INDEX(SPLIT((TEXT( MOD(A1;1); "000000000000000E000"));"E");1;1));(10^(-1*INDEX(SPLIT((TEXT( MOD(A1;1);"000000000000000E000")); "E");1;2)))))
What I would like to do is use symbols like ¼ ½ and ¾ (so that for example the formula converts 1.5 to "1 ½"). These are symbols I copied and pasted from Microsoft Word. They paste into Google spreadsheets fine but as text, so I'm guessing I need to add Concatenate into this formula, but I don't know how.
My value in cell A1 will always be a multiple of 0.25 (e.g. 0.5 or 2 or 3.75 etc), so I will only need the symbols ¼ ½ and ¾.
If anyone know how do this preferably with a formula or otherwise a script I would be very grateful indeed.

Considering that you only have multiples of 0.25, the following formula serves to you. If you have a value that it is not multiple of 0.25, it will fails.
=IF(INT(A1)=A1,A1,IF(INT(A1)>0 ; INT(A1) &" " ; "")&(if(A1-INT(A1)>0.5; "¾"; if(A1-INT(A1)>0.25; "½"; "¼"))))

Various decimal-to-fraction converters have been implemented in Javascript, for example this one.
That function could be imported and used as a custom spreadsheet function. It is a simple extension to replace fractions with the equivalent ASCII or Unicode characters.
A custom function will be far more readable than implementing a complex spreadsheet function.
=dec2frac(A1)

Related

Converting between Bases, and from a String to any Base N in JavaScript (Radix conversion)

First post on here!
I've done a couple hours of research, and I can't seem to find any actual answers to this, though it may be my understanding that's wrong.
I want to convert a string, lets say "Hello 123" into any Base N, lets say N = 32 for simplicity.
My Attempt
Using Javascript's built-in methods (Found through other websites, and):
stringToBase(string, base) {
return parseInt(string, 10).toString(base);
}
So, this encodes the string to base 10 (decimal) and then into the base I want, however the caveat with this is that it only works from 2 to 36, which is good, but not really in the range that I'm looking for.
More
I'm aware that I can use the JS BigInt, but I'm looking to convert with bases as high as 65536 that uses an arbitrary character set that does not stop when encountering ASCII or (yes I'm aware it's completely useless, I'm just having some fun and I'm very persistent). Most solutions I've seen use an alphabet string or array (e.g. "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+-").
I've seen a couple threads that say that encoding to a radix which is not divisible by 2 won't work, is that true? Since base 85, 91, exist.
I know that the methods atob() and btoa() exist, but this is only for Radix/Base 64.
Some links:
I had a look at this github page: https://github.com/gliese1337/base-to-base/blob/main/src/index.ts , but it's in typescript and I'm not even sure what's going on.
This one is in JS: https://github.com/adanilo/base128codec/blob/master/b128image.js . It makes a bit more sense than the last one, but the fact there is a whole github page just for Base 128 sort of implies that they're all unique and may not be easily converted.
This is the aim of the last and final base: https://github.com/qntm/base65536 . The output of "Hello World!" for instance, is "驈ꍬ啯𒁗ꍲ噤".
(I can code java much better than JS, so if there is a java solution, please let me know as well)

String.fromCharCode() does not work after the value "126"

I have been trying the following code to get the ASCII equivalent character
String.fromCharCode("149")
but, it seems to work till 126 is passed as parameter. But for 149, the symbol generated should be
•
128 and beyond is not standard ASCII.
var s = "•";
alert(s.charCodeAt(0))
gives 8226
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode
Getting it to work with higher values Although most common Unicode
values can be represented with one 16-bit number (as expected early on
during JavaScript standardization) and fromCharCode() can be used to
return a single character for the most common values (i.e., UCS-2
values which are the subset of UTF-16 with the most common
characters), in order to deal with ALL legal Unicode values (up to 21
bits), fromCharCode() alone is inadequate. Since the higher code point
characters use two (lower value) "surrogate" numbers to form a single
character, String.fromCodePoint() (part of the ES6 draft) can be used
to return such a pair and thus adequately represent these higher
valued characters.
The fromCharCode() method converts Unicode values into characters.
to use unicode see the link for unicode table
http://unicode-table.com/en/
I got String.fromCodePoint(149) to show inside an alert in firefox but not in IE & Chrome. It may be because of browser language settings.
But this looks correct accourding to the ASCII table.
http://www.asciitable.com/
This is the code I used
alert(String.fromCodePoint(149));

How can I get a massive text file into a JavaScript array?

I'm using a list of words with positive and negative sentiment from AFINN to do some text analysis.
Problem is, the list comes in a .txt file in the following format (word on the left, pos vs neg index at right):
casualty -2
catastrophe -3
catastrophic -4
cautious -1
celebrate 3
celebrated 3
celebrates 3
celebrating 3
To work with it, I need it in the following format:
var array = [{word:"casualty",score:-2},{word:"catastrophe",score:-3},{word:"catastrophic",score:-4}, etc etc]
I'd actually prefer to do this once with a shell script, rather than in the browser. Which is why I'm thinking Node.js could come in handy here. But I'm not very familiar with Node.
Direct link to the zip containing the raw text files.
In case you don't really care about how to read text into a javascript array, and you just need AFINN in JSON, I just found a version here.

Adding commas to large numbers in d3.js

I have data points on a line, when the cursor passes over them, a tooltip div appears with data about that point. The code for the html of the div is below.
div.html("The Avengers Box Office: $" + d.Avengers)
d.Avengers returns 207438708 as a string. So is there any way to add commas to large numbers in d3.js?
I have values ranging from thousands to hundreds of millions and it can be tricky to read them.
Thanks
Solution:
You can't add commas using d3 but you can do via plain JavaScript: Number.toLocaleString()
// convert Avengers to a Number and use the toLocaleString on it
div.html("The Avengers Box Office: $" + (1*d.Avengers).toLocaleString() )
Advanced Solution: If your main problem is that numbers are just too big (even with commas), you need write some really cool conversion functions for your numbers that
count the decimals and
choose a really nice format to display nice numbers
Check out a somehow related answer where I added some code to do that, converting big numbers to "kilo", "mega", "giga", and "terra". You might want to adopt that.
Thanks for the replies guys. I found the answer here. Its not adding commas, but its adding M or K after millions or thousands. Which is actually better I think.
https://groups.google.com/forum/?fromgroups=#!topic/d3-js/YFsSmzu4JZk
d3.format(',')(1999222) //// output=1,999,222

How to convert a long string of numbers to a short string?

I'm building a grid where each box has a different color value using HTML & Javascript.
Example: A 5x5 grid where I'd have 25 values to keep track of. I'd like to be able to send the grid's information via a 'grid' parameter so when any user wants to view this specific grid, they'd see the fully drawn grid. Initially, the URL would be: www.mysite.com?grid=0123401234012340123401234 Ideally, the parameter would be under 25 characters.
How would I go about converting '0123401234012340123401234' to a smaller string? Is it best using a compression algorithm or just using decimal to hex conversion?
You can just use a higher base. Something like 30, for example: (123401234012340123401234).toString(30) --> "8i015nb02ib0c4ik". Note that, for this to work properly, the 123401234012340123401234 has to be a number rather than a string.
If each cell contains a color, that means each cell needs 3 bytes (or 4 if you include changes in the alpha channel). This means that you need 6 hexadecimal characters for each cell if you were encoding in hexadecimal.
This isn't optimal, so I suggest using a-zA-Z0-9 which equals 26 + 26 + 10 = 62 characters . Then use the two characters - and _ as well, to get a full 64 characters. Using hexadecimal, you needed 6 characters to encode 2^(24) possible values (or 2^32 if you are using alpha channel).
With 64-base characters, you only need 4 characters per cell. The reason I am mentioning this is because a-zA-Z0-9 - and _ are valid url characters.
All of this is assuming of course you are using all the colors. If you limit the domain, you can reduce the number of characters per cell drastically, but you should still use the same encoding scheme.

Categories