How to format number in node js? [closed] - node.js

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 months ago.
Improve this question
I want that if i am given a value like 1k then it should be converted to 1000 or 1m to 100000 and so on..
1k - 10000
100k - 100000
1m - 1000000
1b - 1000000000
I have searched a lot on the internet, but I can't find any npm package that can do it as well.
i just want a code solution or a npm package to help me in it.

Maybe you can try this
var thousand = '1k'
var hundredThousand = '100k'
var hundredThousandComa = '13.5k'
var million = '1m'
var billion = '1b'
var arrayFormat = [thousand, hundredThousand, hundredThousandComa, million, billion]
arrayFormat.map((value) => {
var number = value.slice(0, -1)
var last = value.slice(-1)
if (last == 'k') {
console.log(number * 1000)
} else if (last == 'm') {
console.log(number * 1000000)
} else if (last == 'b') {
console.log(number * 1000000000)
}
})

Create a map of units to coefficients.
Parse each input string into its value and unit.
Multiply the value by the corresponding unit's coefficient to get the product.
Example:
const map = {
k: 1e3,
m: 1e6,
b: 1e9,
// etc.
};
const inputs = [
'1k',
'100k',
'1m',
'1b',
];
for (const input of inputs) {
const value = input.slice(0, -1);
const unit = input.slice(-1);
const product = value * map[unit];
console.log(value, unit, product);
}

Related

What is the best way to profile SSD for writes using code?

The question comes cause i am writing a application which is very write intensive(using lsm data) and i should be able to say how many transactions i can get on given speced system.
The problem is world of IOPS and throughout is confusing plus OS of different make have different cache at different levels and so on...
Which made me to write my own application(node.js) and do a part of write intensive workload, time it and derive some conclusion around it but the results are confusing..
My app appends to say one file with chuncks of 145MB 3 times in 5 sec.. but if i spread my appends across 100 files thus dividing the payload by 100 ie 145MB/100 i am able to do that in 4 times in 5 seconds.. what explains this behavior?
Is there any better and consistent way to mathematical derive a formula to given iops of ssd what iops can it sustain for one file or multiple files?
Workload is append only and no forced fsync..
Some pseudo code(not full code):
let OPS = 0, newOPS = 0;
for (spreadFactor = 1; (spreadFactor <= 1024) || spreadFactor === 1); spreadFactor += spreadFactor) {
const payload = Array.from({ length: (this.sampleCapacity / spreadFactor) }, (_, idx) => [idx.toString().padStart(20, "0"), idx.toString().padStart(20, "0"), idx.toString().padStart(20, "0"), idx.toString().padStart(20, “0”]));
OPS = newOPS;
let actualBytesOnDisk = 0;
const st = new Stopwatch()
let requests = 0;
while (st.elapsed() < 5000) {
requests++;
for (let index = 0; index < spreadFactor; index++) {
this.handle = fs.openSync(`${spreadFactor}-${index}`, "as");
fs.appendFileSync(this.handle, payload);
fs.fsyncSync(this.handle);
fs.fdatasyncSync(this.handle);
}
}
const elapsed = st.elapsed();
newOPS = (requests / elapsed) * 1000;
console.info(`Calibrating writes R:${requests} E: ${elapsed} F: ${spreadFactor} RPS:${newOPS.toFixed(3)} S:${payload.length} B:${actualBytesOnDisk}`);
}

The BEST way to read some strings and print the longest one in GO [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I'm getting crazy.
I'm trying to write a program which reads some strings (in my case 5) and prints the longest one.
I have googled and searched here but i haven't fond anything...
package main
import "fmt"
func main() {
var t1, t2, t3, t4, t5 string
fmt.Scan(&t1, &t2, &t3, &t4, &t5)
var l1, l2, l3, l4, l5 int
var max int = -50000
l1 = len(t1)
l2 = len(t2)
l3 = len(t3)
l4 = len(t4)
l5 = len(t5)
var longest string = ""
if l1 > max {
l1 = max
}
if l2 > max {
l2 = max
}
if l3 > max {
l3 = max
}
if l4 > max {
l4 = max
}
if l5 > max {
l5 = max
}
fmt.Println(max)
}
Here I wrote simple codes to do that. follow A Tour of Go as Flimzy said above and get more understanding about Go loops.
package main
import "fmt"
func main() {
t := make([]string, 5)
//scan strings first
for i := range t {
fmt.Scan(&t[i])
}
var longest string = ""
// compare longest
for _, s := range t {
if len(longest) < len(s) {
longest = s
}
}
fmt.Println(`longest := `,longest)
}
You can reduce first loop and do it below way. Scan string and compare in one loop.
package main
import "fmt"
func main() {
t := make([]string, 5)
var longest string = ""
for i, _ := range t {
fmt.Scan(&t[i])
if len(longest) < len(t[i]) {
longest = t[i]
}
}
fmt.Println(`longest := `,longest)
}

How to randomize the case of letters in a string in Swift? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Given a string, I would like to change the case of each letter to a random between upper and lower case.
For example:
let myString = "Have a nice day!"
let newString = "haVe A NiCe dAY!"
Swift 2
I would use arc4random_uniform and map.
let myString = "Have a nice day!"
let result = myString.characters.map { (char) -> String in
if arc4random_uniform(2) == 0 {
return String(char).lowercaseString
}
return String(char).uppercaseString
}.joinWithSeparator("")
print(result)
hAvE A NICe DAy!
Swift 3:
let result = myString.characters.map {
if arc4random_uniform(2) == 0 {
return String($0).lowercased()
}
return String($0).uppercased()
}.joined(separator: "")
Swift 4.2
let result = myString.map {
if Int.random(in: 0...1) == 0 {
return String($0).lowercased()
}
return String($0).uppercased()
}.joined(separator: "")

Generate auto increment number in a textbox using C# [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want to add sequence numbers in textbox but the format is 001 to onward, the main problem is when the number comes to 009, after it became 0010, four digit character. I want to reduce a zero from it, the number should look like 010. Please help me for this problem
This should meet your needs.
for (int i = 0; i <= 100; ++i)
{
Console.WriteLine(string.Format("{0:000}", i));
}
Quick and dirty. Probably not the best way to do this:
textBox1.Text = (Convert.ToInt16(textBox1.Text) + 1).ToString();
if (textBox1.Text.Length == 1)
{
textBox1.Text = "00" + textBox1.Text;
}
else if (textBox1.Text.Length == 2)
{
textBox1.Text = "0" + textBox1.Text;
}

Web Audio filter node popping [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am getting popping when modulating a filter nodes frequency with an LFO when the filter frequency bottoms out.
I suspect this is due to the amplitude not being at the time of cutoff. Is it necessary to modulate the gain as well? I was hoping not to have to and I'm not really sure how I would anyway.
Edit: forgot link to example code http://jsfiddle.net/hyf5N/
var context = new webkitAudioContext();
var lfo = context.createOscillator();
lfo.frequency.value = 10;
var saw = context.createOscillator();
saw.type = 'sawtooth';
var filter = context.createBiquadFilter();
filter.type = 'lowpass';
var gain = context.createGainNode();
gain.gain.value = 500;
saw.connect(filter);
lfo.connect(gain);
gain.connect(filter.frequency);
filter.connect(context.destination);
lfo.start(0);
saw.start(0)
I think you're getting pops (would have to analyze waveform to see exactly why) because your LFO is modulating the frequency from -490 to 510. (freq.value = 10, + LFO ([-1,1]) * 500.) I expect the negative frequency is causing an issue.
If you want it to modulate from 0 to 1kHz, you set the frequency.value to 500, and the gain.value to 500. (The key here is that an oscillator is going to go from -1 to 1, not 0 to 1.)
Code for a rectifier:
var waveshaper = audioContext.createWaveShaper();
var curve = new Float32Array(65536);
for (var i=0; i<32768; i++)
curve[i] = 0.0;
for (var i=32768; i<65536; i++)
curve[i] = (i/32768) - 1;
waveshaper.curve = curve;
I couldn't figure out how to make the waveshaper work for me since I didn't want to bottom out the lfo signal at 0 but rather make it so the modulation wouldn't push the filter frequency below 0. I ended up using a scriptProcessorNode like so
modulationProcessor = this.context.createScriptProcessor(4096);
modulationProcessor.onaudioprocess = function(e) {
// bottom out frequency modulation at filter.frequency == 0 to avoid popping
var input = e.inputBuffer.getChannelData(0);
var output = e.outputBuffer.getChannelData(0);
var frequency = filter.frequency.value;
for (var i = 0; i < input.length; i++) {
output[i] = (input[i] + frequency) > 0 ? input[i] : input[i] - (input[i] + frequency);
}
}
It isn't perfect since it seems like if you are adjusting the filter frequency a lot you can still get popping sometimes. I think it is because the filter value can get stale while the buffers are being written. It seems to work fine if you aren't live editing the filter frequency though.

Resources